excel VBA使用变量调用单元格

yxyvkwin  于 2022-11-18  发布在  其他
关注(0)|答案(4)|浏览(248)

我刚开始玩Excel宏,就已经卡住了。
我有一个主工作表,其中的数据结构如下所示

France    10
Germany   14
US        20

然后我有另外三张纸叫做法国、德国、美国
我的目标是将数字复制到每个相应的工作表中。总是复制到主工作表上定义的同一单元格(只是在不同的工作表上),在单元格O1中,我有= B5,在单元格P1中,我有= 3(因为我需要它的3倍)
我的想法是一行一行地遍历工作表,并且有两个变量

country
value

我设法把法国作为国家,把10作为价值
但是当我试图在循环中做的时候,我得到了这个错误(星星在哪里)
错误1004:对象“global”的方法“range”失败

Sub trial()
Dim destination As String
Dim inputer As Long
Dim country As String
Dim counter As Boolean
Dim maxcounter As Boolean

maxcounter = Range("P1").Value

counter = "1"

While maxcounter > counter:

    
  destination = Range("O1").Value

    **country = Range("A" & counter).Value**

    inputer = Range("B" & counter).Value

    Sheets(country).Range(destination).Value = inputer

    counter = counter + 1
Wend

End Sub
bweufnob

bweufnob1#

我没有试图重现你的问题,但在短暂的看了一下你的代码后,我想知道你为什么要写
尺寸计数器为布尔值尺寸最大计数器为布尔值计数器=“1”
我认为您应该首先将类型从Boolean更改为Integer,然后写入counter = 1(即使VBA可能会以您希望的方式处理此问题,因为类型为Integer)
也许是对未来的一个提示:如果你知道一个循环将被执行多少次,你可以考虑使用For counter ... Next counter而不是counter = 1 While ... counter = counter + 1 ... Wend

j2datikz

j2datikz2#

Option Explicit

Sub trial()

    Dim destination As String
    Dim inputer As Long
    Dim country As String
    Dim counter As Long
    Dim maxcounter As Long
    
    Dim ws As Worksheet
    Set ws = Sheets(1) ' Main Sheet
    
    maxcounter = ws.Range("P1").Value
    destination = ws.Range("O1").Value
    
    counter = 1
    While counter <= maxcounter
    
        country = ws.Range("A" & counter).Value
        inputer = ws.Range("B" & counter).Value
        Sheets(country).Range(destination).Value = inputer
    
        counter = counter + 1
    Wend
End Sub
93ze6v8z

93ze6v8z3#

这里的问题是布尔值。布尔值用于真/假值Windows Boolean Data Type Reference。maxcounter的定义很好,但如果您希望始终对整个表执行此操作,则可以将其更改为自动查找数据集的最后一行,而不是特定的单元格。您还应该至少为包含数据表的工作表定义一个工作表变量。下面是我将如何使用相同的通用设计来实现这一点:

Sub trial()
Dim ws As Worksheet
Dim destination As String
Dim country As String
Dim counter As Long
Dim maxcounter As Long

Set ws = Sheets("Sheet1")
'Sheet1 is placeholder. Replace with whatever name of the sheet is where the data table is.

maxcounter = ws.Range("P1").Value
destination = ws.Range("O1").Value

For i = 1 To maxcounter
    country = ws.Cells(i, 1).Value
    Sheets(country).Range(destination).Value = ws.Cells(i, 2).Value
Next

End Sub
bcs8qyzn

bcs8qyzn4#

复制值

Option Explicit

Sub CopyValues()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet1") ' Source Worksheet
    
    Dim DestinationAddress As String ' (destination)
    DestinationAddress = sws.Range("O1").Value
    
    Dim LastRow As Long ' (maxcounter As Boolean)
    LastRow = sws.Range("P1").Value
    
    Dim r As Long ' (counter As Boolean)
    r = 1
    
    Dim dws As Worksheet ' Each Destination Worksheet (Object)
    Dim dCell As Range ' Each Destination Cell (Object)
    Dim Country As String ' (country)
    Dim Points As Long ' (inputer) ' use more appropriate than 'Points'
    
    Do While r <= LastRow ' 'Do...Loop' is an improved 'While...Wend'
        Country = sws.Range("A" & r).Value
        Points = sws.Range("B" & r).Value
        Set dws = wb.Worksheets(Country)
        Set dCell = dws.Range(DestinationAddress)
        dCell.Value = Points
        r = r + 1
    Loop

    MsgBox "Data copied.", vbInformation

End Sub

相关问题