Excel VBA-工作表/范围引用无法正常工作
我在启动工作表/范围时遇到问题,我不明白为什么我的代码不起作用。我正在调试,似乎我必须在某个工作表上进行相关的引用才能工作。谁能告诉我我做错了什么Excel VBA-工作表/范围引用无法正常工作,excel,vba,Excel,Vba,我在启动工作表/范围时遇到问题,我不明白为什么我的代码不起作用。我正在调试,似乎我必须在某个工作表上进行相关的引用才能工作。谁能告诉我我做错了什么 Sub salesImport() Application.ScreenUpdating = False 'Excel workbook, the source and target worksheets, and the source and target ranges. Dim wbBook As Workbook Dim wsSource
Sub salesImport()
Application.ScreenUpdating = False
'Excel workbook, the source and target worksheets, and the source and target ranges.
Dim wbBook As Workbook
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Dim rnSource As Range
Dim rnTarget As Range
Dim rng As Range
Dim cIndex, rIndex1, rIndex2, rIndex3, iR, iC As Integer
Dim rowC, columnC As Integer
'Initialize the Excel objects
Set wbBook = ThisWorkbook
With wbBook
Set wsSource = .Worksheets("Sales")
Set wsTarget = .Worksheets("Summary-Official")
End With
'On the source worksheet, set the range to the data stored
With wsSource
rowC = .Cells.SpecialCells(xlCellTypeLastCell).row
columnC = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
Set rnSource = .Range(Cells(1, 1), Cells(rowC, columnC))
End With
With wsTarget
Set rnTarget = .Range("B98:AM122")
End With
rIndex1 = 6 'month
rIndex2 = 10 'plant
rIndex3 = 17 'sales
iR = 0
iC = 0
For Each Column In rnSource
Column.Cells(rIndex1, 1).Select
Dim m As String: m = Column.Cells(rIndex1, 1).Value
Select Case Month(DateValue("01 " & m & " 2012"))
Case 1
iC = 6
Case 2
iC = 7
Case 3
iC = 8
Case 4
iC = 9
End Select
iR = findrow2(Column.Cells(rIndex2, 1), rnTarget)
If iR <> 0 Then
rnTarget.Cells(iR - 97, iC).Value = Column.Cells(rIndex3, 1).Value
End If
'MsgBox ("got here")
Next Column
Application.ScreenUpdating = True
End Sub
Sub-salesImport()
Application.ScreenUpdating=False
'Excel工作簿、源和目标工作表以及源和目标范围。
将工作簿作为工作簿
将wsSource设置为工作表
将目标设置为工作表
暗源As范围
弱小目标作为射程
变暗rng As范围
Dim cIndex、rIndex1、rIndex2、rIndex3、iR、iC作为整数
Dim rowC,列C为整数
'初始化Excel对象
设置wbBook=ThisWorkbook
用wbBook
设置wsSource=.Worksheets(“销售”)
Set wsTarget=.Worksheets(“官方摘要”)
以
'在源工作表上,将范围设置为存储的数据
使用wsSource
rowC=.Cells.SpecialCells(xlCellTypeLastCell).row
columnC=.Range(“A1”).SpecialCells(xlCellTypeLastCell).Column
设置rnSource=.Range(单元格(1,1),单元格(行,列))
以
有目标
设置rnTarget=.Range(“B98:AM122”)
以
Rindex 1=6'月
rIndex2=10'装置
Rindex 3=17'销售额
iR=0
iC=0
对于rnSource中的每个列
Column.Cells(rIndex1,1)。选择
作为字符串的Dim m:m=列.Cells(rIndex1,1).Value
选择案例月份(日期值(“01”&m&“2012”))
案例1
iC=6
案例2
iC=7
案例3
iC=8
案例4
iC=9
结束选择
iR=findrow2(Column.Cells(rIndex2,1),rnTarget)
如果iR为0,则
rnTarget.Cells(iR-97,iC).Value=列.Cells(rIndex3,1).Value
如果结束
'MsgBox(“到达这里”)
下一栏
Application.ScreenUpdating=True
端接头
谢谢
With wsSource
'...
Set rnSource = .Range(Cells(1, 1), Cells(rowC, columnC))
End With
应该是
With wsSource
'...
Set rnSource = .Range(.Cells(1, 1), .Cells(rowC, columnC))
End With
…否则,
Cells()
将调用activesheet的默认值,而activesheet可能不是wsSource
重要注意:列
未定义。将选项Explicit
添加到模块顶部并声明所有变量。也可以考虑使用不同的变量名。注意<代码> DimeRoC,CalnC作为整型< /COD>只声明<代码> CalcNC为整数< /Calp>,但是<代码> ROWC作为变体。在VBA中,您需要为每个变量指定一种类型,否则默认为Variant
:Dim rowC As Long,columnC As Long
。此外,行计数变量需要是长的
,因为Excel中的行数超过了整数
。我推荐使用VBA,因为在VBA中使用Integer
没有任何好处。我没有看到一个可以接受答案的区域,但是在使用3个回复中的建议修改了我的代码后,它现在可以工作了。谢谢大家!