如何使用特定文本而不是列来粘贴我的公式?

时间:2015-06-15 20:10:04

标签: excel vba excel-vba excel-formula

我几乎有一个已经工作的宏,但是对于未来它可能会导致问题,因为宏我已经找到了我给它的列,然后开始在那里输入公式。现在我的数据可能在将来发生变化,在该列中我可能会有新的内容,因此宏显然会将公式运行到错误的列。手动更改它是可能的,但忙碌和很多工作。有没有可能的方法我可以选择一个特定文本而不是列的单元格?由于文本永远不会改变,这对我来说更容易使用。因为这样做,公式将始终发布在正确的列中。

EDIT!我将整个代码添加到帖子中,这样您就可以更清楚地看到它,并更清楚地理解我的意思。

Sub HW_Copy_RawData_Formulas()
Dim intChoice As Integer
Dim strPath As String
Dim I As Integer
Dim filePath As String
Dim SourceWb As Workbook
Dim TargetWb As Workbook
Dim Lastrow As Long
Dim Nrow As Long

Set TargetWb = ActiveWorkbook
' Delete Rows
On Error Resume Next
    TargetWb.Worksheets("Raw Data").Activate
    Range("A2:AL2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Range("A2:AL2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
'Copy Formulas
Range("AF2").Formula = "=IF([@ServDt]<DATE(2013,1,1), DATE(YEAR([@ServDt]),12,31),EOMONTH([@ServDt],0))"
Range("AG2").Formula = "=IF([@Amount]>1,[@Quantity],0)"
Range("AH2").Formula = "=IF([@Amount]<>0,[@Amount]-[@Adj]-[@[Adjustment ]],0)"
Range("AI2").Formula = "=IF(AND([@Department]=""HH"",[@Pay]=0),[@Amount]/2,0)"
Range("AJ2").Formula = "=IF([@Amount]<>0,[@Bal]-[@[Adjustment ]],[@Bal]+[@Adj])"
Range("AK2").Formula = "=VLOOKUP([Department],Service[#All],2,FALSE)"
Range("AL2").Formula = "=VLOOKUP([@Entity],Site,3,FALSE)"

MSG1 = MsgBox("Add Raw Data", vbYesNo)
If MSG1 = vbYes Then

'only allow the user to select one file
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
    If intChoice <> 0 Then
    'get the file path selected by the user
        strPath = Application.FileDialog( _
            msoFileDialogOpen).SelectedItems(1)
     Else: GoTo endmsg
    End If

'Setting source of data
Set SourceWb = Workbooks.Open(strPath)

Lastrow = SourceWb.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row

SourceWb.Worksheets(1).Range("A2:BJ" & Lastrow).SpecialCells(xlCellTypeVisible).Select
Selection.Copy Destination:=TargetWb.Sheets("Raw Data").Range("A2")
' Close the source workbook without saving changes.
SourceWb.Close savechanges:=False

Else
endmsg:
  MsgBox "Complete"
End If

Range("AF2:AL2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AF2").PasteSpecial xlPasteValues

End Sub

1 个答案:

答案 0 :(得分:0)

以下代码段可能对您有用。它获取给定特定值的单元格范围。它也可以用来搜索.Rows()的特定行。

Dim *YOURCELL* As Range

Set *YOURCELL*= .Columns(1).Find(What:= *WHATYOUWANTTOFIND*, LookAt:=xlWhole, MatchCase:=False, searchformat:=False)

但是,如果您不知道上次使用的单元格的位置,请考虑阅读this其他帖子。

编辑:

只要当前选定的单元格不为空,while循环就会运行。在此循环中,它选择右侧的下一个单元格并递增计数。循环完成后,当前选定的单元格是第二行中的第一个空单元格。 Count通过在循环旁边递增来找到它的列号,因此可以根据需要使用它。之后我使用了单元格而不是范围,因为它可以使用列号。

Range("A2").Select    

Dim count As Integer
count = 1

'skip all used cells in the row
Do While Not (ActiveCell.value = None)
    ActiveCell.Offset(0, 1).Range("A1").Select
    count = count + 1
Loop

Cells(count, 2).Formula = your_formula
Cells(count + 1, 2).Formula = your_formula ' next cell to the right
Cells(count + 2, 2).Formula = your_formula ' next cell to the right