Excel 2010 VBA在其他工作表中引用特定单元格

时间:2011-10-14 10:06:00

标签: excel vba

对不起,这可能是一个非常基本的Excel VBA问题。我刚开始学习它,而且我没有找到很好的教程。实际上我找不到关于语言的有组织的信息。

我有一些名为“Sheet1”和“Sheet2”的工作表 Sheet1的第一列和第二列包含一些数字。

我想编写一个宏来打印函数的结果,该函数将2个变量(从A列和B列各一个)传递给Sheet2。但我想在新工作表中将这些结果分开,以便Sheet1中Column'i'上的函数结果放入第1行和第4列* i。这是我到目前为止尝试做的事情,但它没有奏效,因为我不知道如何正确引用其他工作表中的特定单元格。

道歉,这是一个非常新的问题,非常感谢任何帮助!

Sub results()

    Dim i As Integer, noValues As Integer
    noValues = Application.CountA(Range("A:A"))

    Sheets("Sheet2").Select
    Range("A1").Select
    For i = 1 To noValues
        Range("A1").Offset(0, 4 * (i - 1)).Select
        ActiveCell.FormulaR1C1 = "=Sheet1!A[i] + Sheet1!B[i]"
    Next i

End Sub

其中A [i]和B [i]应表示A列或B列中的值,即行i。

3 个答案:

答案 0 :(得分:5)

Sub Results2()

    Dim rCell As Range
    Dim shSource As Worksheet
    Dim shDest As Worksheet
    Dim lCnt As Long

    Set shSource = ThisWorkbook.Sheets("Sheet1")
    Set shDest = ThisWorkbook.Sheets("Sheet2")

    For Each rCell In shSource.Range("A1", shSource.Cells(shSource.Rows.Count, 1).End(xlUp)).Cells
        lCnt = lCnt + 1
        shDest.Range("A4").Offset(0, lCnt * 4).Formula = "=" & rCell.Address(False, False, , True) & "+" & rCell.Offset(0, 1).Address(False, False, , True)
    Next rCell

End Sub

这循环遍历sheet1的A列,并在sheet2中为每个单元格创建一个公式。要查找Sheet1中的最后一个单元格,我从底部(shSource.Rows.Count)和.End(xlUp)开始,以获取列中非空白的最后一个单元格。

要创建公式的元素,我使用Sheet上单元格的Address属性。我正在使用三个参数来解决。前两个是RowAbsolute和ColumnAbsolute,都设置为false。我不关心第三个参数,但我将第四个参数(外部)设置为True,以便它包含工作表名称。

我更喜欢从源到目的地而不是其他方式。但这只是个人偏好。如果您想在目的地工作,

Sub Results3()

    Dim i As Long, lCnt As Long
    Dim sh As Worksheet

    lCnt = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("Sheet1").Columns(1))
    Set sh = ThisWorkbook.Sheets("Sheet2")

    Const sSOURCE As String = "Sheet1!"

    For i = 1 To lCnt
        sh.Range("A1").Offset(0, 4 * (i - 1)).Formula = "=" & sSOURCE & "A" & i & " + " & sSOURCE & "B" & i
    Next i

End Sub

答案 1 :(得分:2)

我将给你一个简单的答案,希望能帮助你解决VBA问题。了解VBA如何工作以及如何引用和访问元素的最简单方法是记录宏,然后在VBA编辑器中对其进行编辑。这就是我学习VBA的方式。它基于visual basic,因此VB的所有编程约定都适用。录制宏可以让您了解如何访问和执行操作。

你可以使用这样的东西:

var result = 0
Sheets("Sheet1").Select
result = Range("A1").Value * Range("B1").Value
Sheets("Sheet2").Select
Range("D1").Value = result

或者您也可以使用Cells(1,1).Value引用单元格。这样您就可以根据需要设置变量并递增它们。我想我不知道你到底想要做什么,但我希望这会有所帮助。

答案 2 :(得分:0)

Private Sub Click_Click()

 Dim vaFiles As Variant
 Dim i As Long

For j = 1 To 2
vaFiles = Application.GetOpenFilename _
     (FileFilter:="Excel Filer (*.xlsx),*.xlsx", _
     Title:="Open File(s)", MultiSelect:=True)

If Not IsArray(vaFiles) Then Exit Sub

 With Application
  .ScreenUpdating = False
  For i = 1 To UBound(vaFiles)
     Workbooks.Open vaFiles(i)
     wrkbk_name = vaFiles(i)
    Next i
  .ScreenUpdating = True
End With

If j = 1 Then
work1 = Right(wrkbk_name, Len(wrkbk_name) - InStrRev(wrkbk_name, "\"))
Else: work2 = Right(wrkbk_name, Len(wrkbk_name) - InStrRev(wrkbk_name, "\"))
End If



Next j

'Filling the values of the group name

'check = Application.WorksheetFunction.Search(Name, work1)
check = InStr(UCase("Qoute Request"), work1)

If check = 1 Then
Application.Workbooks(work1).Activate
Else
Application.Workbooks(work2).Activate
End If

ActiveWorkbook.Sheets("GI Quote Request").Select
ActiveSheet.Range("B4:C12").Copy
Application.Workbooks("Model").Activate
ActiveWorkbook.Sheets("Request").Range("K3").Select
ActiveSheet.Paste


Application.Workbooks("Model").Activate
ActiveWorkbook.Sheets("Request").Select

Range("D3").Value = Range("L3").Value
Range("D7").Value = Range("L9").Value
Range("D11").Value = Range("L7").Value

For i = 4 To 5

If i = 5 Then
GoTo NextIteration
End If

If Left(ActiveSheet.Range("B" & i).Value, Len(ActiveSheet.Range("B" & i).Value) - 1) = Range("K" & i).Value Then
    ActiveSheet.Range("D" & i).Value = Range("L" & i).Value
 End If

NextIteration:
Next i

'eligibles part
Count = Range("D11").Value
For i = 27 To Count + 24
Range("C" & i).EntireRow.Offset(1, 0).Insert
Next i

check = Left(work1, InStrRev(work1, ".") - 1)

'check = InStr("Census", work1)
If check = "Census" Then
workbk = work1
Application.Workbooks(work1).Activate
Else
Application.Workbooks(work2).Activate
workbk = work2
End If

'DOB
ActiveWorkbook.Sheets("Sheet1").Select
ActiveSheet.Range("D2").Select
ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Application.Workbooks("Model").Activate
ActiveWorkbook.Sheets("Request").Select
ActiveSheet.Range("C27").Select
ActiveSheet.Paste

'Gender
Application.Workbooks(workbk).Activate
ActiveWorkbook.Sheets("Sheet1").Select
ActiveSheet.Range("C2").Select
ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Application.Workbooks("Model").Activate
ActiveWorkbook.Sheets("Request").Select
'Application.CutCopyMode = False

ActiveSheet.Range("k27").Select
ActiveSheet.Paste

For i = 27 To Count + 27
ActiveSheet.Range("E" & i).Value = Left(ActiveSheet.Range("k" & i).Value, 1)
Next i

'Salary
Application.Workbooks(workbk).Activate
ActiveWorkbook.Sheets("Sheet1").Select
ActiveSheet.Range("N2").Select
ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Application.Workbooks("Model").Activate
ActiveWorkbook.Sheets("Request").Select
'Application.CutCopyMode = False

ActiveSheet.Range("F27").Select
ActiveSheet.Paste


ActiveSheet.Range("K3:L" & Count).Select
selction.ClearContents
End Sub