Run time Error '1004' using .Formula - excel

I'm building an master excel file that is designed to gather data from lots of other excel files that are stored in the business Dropbox files and place them in the 2nd sheet of the master file. I built a original version on my local computer and that worked perfectly (the path3 variable) but once I tried to convert it based on a changing file path (because each user will have a different path from their PC) I am getting the run time error. The formula defined by path2 is what I have been trying to use but even though the variable seems to be holding the right value (I tested it by having it write out the values) it doesn't seem to be able to move the data, throwing the above error and highlighting the "rngdest.Formula = Chr(61) & path2" line. I really don't have any idea what is causing this and I have spent several days trying different approaches but to no avail so any ideas, solutions or links to already solved (I have spent a long time searching but haven't found anything) would be very much appreciated.
I've included the whole of the code for completeness, I think I've removed most of the redundant code that I left in but there may be some still left. If you need any clarifications on the code please let me know. Thanks for any potential help
Private Sub CommandButton2_Click()
Dim counter As Integer
Dim i As Long
Dim j As Long
Dim k As Long
Dim l As Long
Dim a As Integer
Dim z As Integer
Dim y As Integer
Dim p As Integer
Dim Names() As String
Dim Fix1() As String
Dim path3 As String
Dim path2 As String
Dim SheetName As String
Dim c As Range
Dim found As Range
Dim BookName As String
Dim var1 As String
Dim rngdest As Range
Dim rngsource As Range
Dim cell As String
Dim adjust As Integer
Dim adjust2 As Integer
Dim rngname As Range
Dim colNo As Integer
Dim fin As String
Dim fin2 As String
Dim fin3 As String
Dim comp As String
Dim teststring As String
Dim currentWb2 As Workbook
Set currentWb2 = ThisWorkbook
MsgBox "Excel will now update the sheet, please be patient as this can take a few minutes. You will be notified once it is complete"
ReDim Fix1(1 To 4)
Fix1(1) = "A-F"
Fix1(2) = "G-L"
Fix1(3) = "M-R"
Fix1(4) = "S-Z"
counter = 0
With ActiveSheet
i = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
ReDim Names(1 To i, 1 To 4)
With ActiveSheet
For k = 1 To 4
For a = 2 To i
Names(a, k) = Cells(a, k).Value
Next a
Next k
End With
SheetName = "Analysis"
BookName = "Outcomes Final.xlsm"
For p = 1 To 4
fin2 = Split(Cells(, p).Address, "$")(1)
With ActiveSheet
l = .Cells(.Rows.Count, fin2).End(xlUp).Row
End With
For z = 1 To l
counter = counter + 1
fin = Split(Cells(, counter).Address, "$")(1)
currentWb2.Sheets("Sheet2").Range("" & fin & "1") = Names(z, p)
For y = 1 To 34
adjust = y + 1
cell = "$B$" & y & ""
If z = 1 Then
Else
teststring = GetPath()
teststring = teststring & "\Clients\"
path3 = "'C:\Users\Lewis\Documents\Outcomes\Floating Support\Clients\" & Fix1(p) & "\" & Names(z, p) & "\[Outcomes Final.xlsm]Analysis'!" & cell & ""
path2 = teststring & Fix1(p) & "\" & Names(z, p) & "\Outcomes\[Outcomes Final.xlsm]Analysis'!" & cell & ""
End If
Set rngdest = currentWb2.Sheets("Sheet2").Range("" & fin & "" & adjust & "")
Set rngsource = Range("B" & y & "")
rngdest.Formula = Chr(61) & path2
Next y
Next z
Next p
currentWb2.Sheets("Sheet2").Columns(1).EntireColumn.Delete
currentWb2.Sheets("Sheet1").Range("A1:D35").Interior.ColorIndex = 0
For j = 1 To counter
fin3 = Split(Cells(, j).Address, "$")(1)
If currentWb2.Sheets("Sheet2").Range("" & fin3 & "35") = "1" Then
With currentWb2.Sheets("Sheet1").Range("A1:D35")
comp = currentWb2.Sheets("Sheet2").Range("" & fin3 & "1")
Set c = .Find(comp, LookIn:=xlValues)
If Not c Is Nothing Then
c.Interior.ColorIndex = 3
End If
End With
End If
Next j
MsgBox "The update is now complete, please click on sheet 2 to view the data. All clients in red have not been properly completed"
End Sub

Related

Why am I getting an object required error on my array variant?

I pass data into an array based on when column value <> column value. The array is formed fine, but when its about to move the array to a template, it gives me an object required error. This is brand new and was not erroring out before, what could fix this?
Getting error on this line:
Dest.Offset(j,a) = Data(i,k)
Rest of Code:
Option Explicit
Sub Main()
Dim Wb As Workbook
Dim Data, Last
Dim i As Long, j As Long, k As Long, a As Long
Dim Dest As Range
Dim BASEPATH As String
Dim template As String
template = "M:\.xlsx"
BASEPATH = "M:\"
Set Wb = Workbooks.Open(Filename:=template)
Set Dest = Wb.Sheets("").Range("A3")
With ThisWorkbook.Sheets(1)
Data = .Range("BQ3", .Range("A" & Rows.Count).End(xlUp))
End With
Wb.Activate
For i = 1 To UBound(Data)
If Data(i, 10) <> Last Then
If i > 1 Then
Dest.Select
Wb.SaveCopyAs BASEPATH & _
ValidFileName(Last & "_YE_Planning_File.xlsx")
End If
With Wb.Sheets("")
.Rows(3 & ":" & .Rows.Count).Delete
End With
Last = Data(i, 10)
j = 0
End If
a = 0
For k = 1 To UBound(Data, 2)
Dest.Offset(j, a) = Data(i, k)
a = a + 1
Next
j = j + 1
Next
End Sub
Dest gets deleted.
Set Dest = Wb.Sheets("Pay for Performance Detail").Range("A3")
...
With Wb.Sheets("Pay for Performance Detail")
.Rows(3 & ":" & .Rows.Count).Delete <~ this includes A3, so `Dest` is deleted
End With
Move the Set Dest to after you do the deletion.
Better yet, don't Delete within a loop? (or maybe just ClearContents, as apparently you already had previously)

Use .Formula to Sum a Changing Range of Cells

I am trying to fill out a table by summing the first "x" number of values then the next "x" number of values.
Essentially this:
=SUM(Ax:Ay) where x and y are the row numbers
Sheets(1).Range("B"+i).Formula = "=SUM(A" & x & ":A" & y & ")"
Here is what my actual code looks like:
Private Sub ScrollBar1_Change()
Application.ScreenUpdating = False
Dim slider_val As Long
Dim output_tbl As ListObject
Dim data_tbl As ListObject
Dim i As Long
Dim j As Long
Dim start As Long
Dim finish As Long
Set data_tbl = Sheets("Data").ListObjects("DataTable")
Set output_tbl = Sheets("Slider").ListObjects("OutputTable")
slider_val = Sheets("Slider").Range("A5").Value
start = 2
For i = 1 To 12
finish = start + slider_val
Sheets("Slider").Range("B" & j).Formula = "=SUM(Data!K" & start & ":K" & finish & ")"
j = j + 1
start = finish + 1
Next i
Application.ScreenUpdating = True
End Sub
I get the error "Application-defined or object-defined error" when I try to run this code.
Thank you all for your help, the problem was that I had not defined "j" yet.

Tricky Filldown with Excel VB

I'm creating a column of custom urls (for import into Mailchimp) and just ran into something that is too tricky for me.
I am building a url column that takes elements from other cells. Almost all the parts of the url will be the same except one (the "Role") variable. Here is how the final url will look where the parts in bold are the variables being fed in from the spreadsheet:
http://domain.com/varPath/?PartName=varEmployee&ClientID=varClient&Role=varRole
The url column fills down the same number of rows that are in column A. Below is a list of the pertinent columns/cells in the spreadsheet for clarification:
Column A = email of all employees completing form
Cell B2 = first name (always using first entry for "varEmployee" in the url)
Cell C2 = last name (same as above)
Column D = varClient (stays the same)
Column E = varRole (THIS IS THE TRICKY ONE since I need to get the changing value)
cell I2 = varPath
The "varEmployee" variable is always going to be the same (B2 + C2)
The same is true of varClient and varPath since we only need the value from the second row (under headers). However the "varRole" variable is going to change with each row since each email in column A is associated with a different role in column E. I'm not sure how to get that value into the url string since it keeps changing. My code is below if anyone has any ideas. Thanks in advance.
Dim lngLastRow As Long
Dim varURL As String
Dim varSurvey As String
Dim varPart As String
Dim varEmployee As String
Dim varRole As String
Dim varClient As String
varURL = "https://domain.com/" + Range("I2").Text
varSurvey = Range("I2").Value
varPart = "?PartName="
varEmployee = Range("B2") + " " + Range("C2")
varRole = "&Role=" + Range("E2")
varClient = "&ClientID=" + Range("D2").Text
varFinal = (varURL & varSurvey & varPart & varEmployee & "&ClientID=" & varClient)
lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("F2:F" & lngLastRow).Value = voxFinal
Ok, I read a few more 'for next' posts after seeing Rob's reply and I changed the approach and wrote a slightly different script. This is what I ended up cobbling together which seems to work:
Sub buildURL()
'
' buildURL Macro
'
'
Dim N As Long, i As Long, j As Long
Dim voxURL As String
Dim varSurvey As String
Dim varPart As String
Dim varEmployee As String
Dim varRole As String
Dim varClient As String
varURL = "https://domain.com/" + Range("I2").Text
varSurvey = Range("I2").Value
varPart = "?PartName="
varEmployee = Range("B2") + " " + Range("C2")
varRole = "&Role="
varClient = "&ClientID=" + Range("D2").Text
varFinal = (varURL & varSurvey & varPart & varEmployee & varClient & varRole)
N = Cells(Rows.Count, "A").End(xlUp).Row
j = 2
For i = 2 To N
If Not IsEmpty(Range("A" & i).Value) Then
Cells(j, "F").Value = voxFinal & Cells(i, "E").Value
j = j + 1
End If
Next i
End Sub

VBA Runtime Error 9 when checking whether String has two parts

I am working with cells in a column, which have to be split. Element 1 of the string is supposed to be posted separately from Element 2 of the same string, each on another Worksheet.
String "123 ABC" -> "123" in column C and "ABC" in column D
I am running into a Runtime-Error 9 "Index out of Range" if one of the cells I am checking only contains "123" or "ABC" but no both parts.
I tried to work around it in the way you see in my code below. Needless to say it does not work.
Could one of the more experienced Excel-Gurus help me out here?
Thank you in advance for your time!
Application.ScreenUpdating = False
Dim wbInput As Workbook, wbOutput As Workbook
Set wbOutput = ActiveWorkbook
Dim wsInput As Worksheet, wsOutput As Worksheet, wsMistakes As Worksheet
Set wsOutput = wbOutput.Worksheets("FehlerVorkommen")
Set wsMistakes = wbOutput.Worksheets("NichtZuweisbar")
Dim lRowInput As Long, lRowOutput As Long, lRowMistakes As Long
Dim Lieferant As Range
Dim InputFile As String, myElements() As String
lRowOutput = wsOutput.Range("A" & Rows.Count).End(xlUp).Row
wsOutput.Range("A2:G" & lRowOutput).Clear
wsMistakes.Range("A2:G500").Clear
InputFile = Application.GetOpenFilename()
If InputFile = "Falsch" Then
Exit Sub
End If
Set wbInput = Workbooks.Open(InputFile)
Set wsInput = wbInput.Worksheets("owssvr")
lRowInput = wsInput.Range("A" & Rows.Count).End(xlUp).Row
'Get all Information
For Each Lieferant In wsInput.Columns(1).Rows("2:" & lRowInput)
If wsInput.Columns(3).Rows(Lieferant.Row) <> vbNullString Then
myElements = Split(wsInput.Columns(3).Rows(Lieferant.Row).Value, " ", 2) 'A maximum of 2 String-Parts to avoid 4-5 splits whenever there is a GmbH or AG or whatever
If IsEmpty(myElements(1)) = True Then <<<<<<<<<ERROR HERE<<<<<<<<<<<
lRowMistakes = wsMistakes.Range("A" & Rows.Count).End(xlUp).Row
NextRow = lRowMistakes + 1
wsInput.Columns(1).Rows(Lieferant.Row).Copy Destination:=wsMistakes.Columns(1).Rows(NextRow)
NextRow = NextRow + 1
Else
If IsNumeric(wsInput.Columns(1).Rows(Lieferant.Row)) = True And wsInput.Columns(1).Rows(Lieferant.Row) <> vbNullString _
And IsNumeric(wsInput.Columns(2).Rows(Lieferant.Row)) = True And wsInput.Columns(2).Rows(Lieferant.Row) <> vbNullString Then
wsInput.Columns(1).Rows("2:" & lRowInput).Copy Destination:=wsOutput.Columns(1).Rows("2:" & lRowInput) 'Task Namen
wsInput.Columns(2).Rows("2:" & lRowInput).Copy Destination:=wsOutput.Columns(2).Rows("2:" & lRowInput) 'Bestellpositionen
wsOutput.Columns(3).Rows(Lieferant.Row).Value = myElements(0) 'ID
wsOutput.Columns(4).Rows(Lieferant.Row).Value = myElements(1) 'Name
wsInput.Columns(3).Rows("2:" & lRowInput).Copy Destination:=wsOutput.Columns(5).Rows("2:" & lRowInput) 'Fehlerarten
Else 'Get all wrong inputs on separate Sheet
lRowMistakes = wsMistakes.Range("A" & Rows.Count).End(xlUp).Row
NextRow = lRowMistakes + 1
wsInput.Columns(1).Rows(Lieferant.Row).Copy Destination:=wsMistakes.Columns(1).Rows(NextRow)
NextRow = NextRow + 1
End If
End If
Else 'Get all wrong input on separate Sheet
lRowMistakes = wsMistakes.Range("A" & Rows.Count).End(xlUp).Row
NextRow = lRowMistakes + 1
wsInput.Columns(1).Rows(Lieferant.Row).Copy Destination:=wsMistakes.Columns(1).Rows(NextRow)
NextRow = NextRow + 1
End If
Next Lieferant
wbInput.Close
This line doesn't do what you think it's doing:
If IsEmpty(myElements(1)) = True
First, specifying a limit for the Split function doesn't mean that you always get that many elements in the array. Second, IsEmpty tests to see if a Variant is type VT_EMPTY, not whether a String has a value (Split returns a strongly typed array).
Just test the UBound instead:
If UBound(myElements) > 0 Then

Need code to be able to Check through current column and another column, and Match to use the same order number

if you see below, I have 3 columns, all i basically want is to check column A and Column B, if the carrier (Column A) and Date (Column B) are equal then it will have the same Order Number.
For example: In this case, A3 = A6 and B3 = B6 , so it should have the same order number as one above (160) not 163. I hope this makes it clear.
Thanks for the help. I appreciate it :)
This was quite interesting, so I went ahead and wrote some code. Copy this into a new module and change the sheetname etc. to fit to your workbook. You may also need to redefine fr (firstrow, currently set to 2). The code also currently marks all the changed order-numbers red with the line .Range("C" & r).Font.ColorIndex = 3. Delete / comment it, if you don't want that.
Sub matching()
Dim wb As Workbook
Dim tws As Worksheet
Dim keys() As String
Dim tmpKey As String
Dim pos As Integer
Dim fr, lr As Integer 'first row, last row of data
Set wb = ThisWorkbook
Set tws = wb.Worksheets("Vigmo")
fr = 2
lr = tws.Range("A1000000").End(xlUp).Row
ReDim keys(1 To lr - 1)
With tws
keys(1) = .Range("A" & fr).Value & "_" & .Range("B" & fr).Value
End With
For r = fr + 1 To lr
With tws
tmpKey = .Range("A" & r).Value & "_" & .Range("B" & r).Value
If UBound(Filter(keys, tmpKey)) >= 0 And tmpKey <> "_" Then
'found in array -> replace orderNumber
'On Error resume next
pos = Application.Match(tmpKey, keys, 0)
'On Error goto 0
.Range("C" & r).Value = .Range("C" & pos + 1).Value
.Range("C" & r).Font.ColorIndex = 3
Else
'not found -> next
End If
keys(r - 1) = tmpKey
End With
Next r
End Sub
Let me know if you have any questions as to how this code works!
Below is some code that I came up with that does what your looking for. I dont know how you are generating your order numbers but I assumed they are already present. Hope this helps you :)
Sub OrderNumber()
Dim SearchTerm As String
Dim DateTerm As Date
Dim NumberOfEntries As Long
Dim wks As Excel.Worksheet
Set wks = Worksheets("Sheet1") '<==== Sets the workbook. change it to what yours is called
NumberOfEntries = Application.WorksheetFunction.CountA(wks.Range("A:A")) '<=== Find the number of entries
For x = 2 To NumberOfEntries '<==== Goes through all the entries
SearchTerm = wks.Cells(x, 1) '<===== The Search term (Carrier)
DateTerm = CDate(wks.Cells(x, 2)) '<==== The search Date
For y = x To NumberOfEntries '<===== goes through everything below the search term to speed things up
If wks.Cells(y, 1) = SearchTerm And CDate(wks.Cells(y, 2)) = DateTerm Then '<=== If the name and the date match then
wks.Cells(y, 3) = wks.Cells(x, 3) '<==== Copy the order number
End If
Next y
Next x
End Sub
Just put this in a module or wherever you want but I made it in a module.
G

Resources