这篇文章实际上有两个问题。提前谢谢。
我的第一个问题是,我似乎无法让Excel选择名为"(DATES)-Usage"的Excel工作簿,其中(DATES)每天都会更改。
第二个问题是.Replace
对象正在给我和Run-Time error '438': Object doesn't support this property method
。有人可以看看,然后告诉我在哪里犯错误。再次感谢你!
Sub UsersBySource_Update()
' Create variables
Dim ws As Worksheet
Dim wb As Workbook
Dim unspecified As String
Dim organic As String
Dim TheString As String, Cohort As Date
' Ask user for cohort date and return an error if invalid date is entered
TheString = Application.InputBox("Please Specify The Cohort:")
If IsDate(TheString) Then
Cohort = DateValue(TheString)
Else
MsgBox ("Invalid Date")
End If
' Set variables
unspecified = "[Unspecified]"
organic = "Organic"
' Activate the workbook
For Each wb In Workbooks
If wb.Name Like "*-Usage" Then
wb.Activate
End If
Next
' Find and Replace the unspecified and organic variables in the workbook
ActiveSheet.Replace What:=unspecified, Replacement:=organic, LookAt _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
' Add Columns and Row to fix formatting to continue
Columns("B:E").Insert shift:=xlToRight
Range("B2").EntireRow.Insert
' Copy and Paste initial date range to start program
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
End Sub
答案 0 :(得分:1)
您需要更改
ActiveSheet.Replace What:=unspecified, Replacement:=organic, LookAt _ :=xlPart, SearchOrder:=xlByRows, MatchCase:=False
以下
ActiveSheet.Cells.Replace What:=unspecified, Replacement:=organic, LookAt _ :=xlPart, SearchOrder:=xlByRows, MatchCase:=False
答案 1 :(得分:0)
现在您说要查找工作表,就像(DATES)-Usage ,但您正在查找工作簿:
For Each wb In Workbooks
If wb.Name Like "*-Usage" Then
wb.Activate
End If
Next
尝试这样:(已编辑)
For Each ws In Worksheets
If ws.Name Like "*-Usage" Then
Exit For
End If
Next
符合MSDN:Replace,是一种Range方法。
所以不要这样:
ActiveSheet.Replace What:=unspecified, Replacement:=organic, LookAt _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
试试这个:
ws.Cells.Replace What:=unspecified, Replacement:=organic, LookAt _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
干杯。
答案 2 :(得分:0)
更改这些行
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=False
到这些行
With ActiveSheet
.Range(Range("A3"), Range("A3").End(xlDown)).Copy
.Range("B3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True
End With
Application.CutCopyMode = False
您不需要所有Select
方法。