Please help HLookup from one workbook to another for n number of iteration
Windows(Files).Activate
Sheets("Total").Select
Set myrange = Sheets("Total").Range("1:5")
Windows(ThisFileName).Activate
Sheets("Acc").Select
Range("B" & n + 2, "V" & n + 2).Select
ActiveRange("B" & n + 2, "V" & n + 2).Formula = HLookup(Acc.Range("B" & n + 1, "V" & n + 1), myrange, 2, False)
You need to construct a string representing the worksheet formula.
Set myrange = workSheets("Total").Range("1:5")
with workSheets("Acc").Range("B" & n + 2, "V" & n + 2)
.Formula = "=hlookup(" & .address & ", " & myrange.address(external:=true) & ", 2, False)"
end with
Related
from some googling I found this function that will concatenate the data in columns A, B & C based off the value in column D. This code does not work for me for some reason. My data looks like such
Bob Jason 0123456789 Tim
Jim Jason 0123456789 Tim
Fred Jason 0123456789 Tim
Columns, A and B concat fine, but column C concats to
12,345,678,901,234,500,000,000,000,000
How would the VBA be altered so that the code will concatenate properly?
Sub Concat()
Dim x, i As Long, ii As Long
With Cells(1).CurrentRegion
x = .Columns("d").Offset(1).Address
x = Filter(Evaluate("transpose(if(countif(offset(" & x & ",,,row(1:" & .Rows.Count & "))," & x & ")=1," & x & "))"), False, 0)
For i = 0 To UBound(x)
For ii = 1 To 3
Cells(i + 2, ii + 5).Value = Join(Filter(Evaluate("transpose(if(" & .Columns(4).Address & "=""" & _
x(i) & """," & .Columns(ii).Address & "))"), False, 0), ",")
Next
Cells(i + 2, ii + 5).Value = x(i)
Next
End With
End Sub
You need to set the destination cells to a Text format:
Sub Concat()
Dim x, i As Long, ii As Long
With Cells(1).CurrentRegion
x = .Columns("d").Offset(1).Address
x = Filter(Evaluate("transpose(if(countif(offset(" & x & ",,,row(1:" & .Rows.Count & "))," & x & ")=1," & x & "))"), False, 0)
For i = 0 To UBound(x)
For ii = 1 To 3
Cells(i + 2, ii + 5).NumberFormat = "#"
Cells(i + 2, ii + 5).Value = Join(Filter(Evaluate("transpose(if(" & .Columns(4).Address & "=""" & _
x(i) & """," & .Columns(ii).Address & "))"), False, 0), ",")
Next
Cells(i + 2, ii + 5).NumberFormat = "#"
Cells(i + 2, ii + 5).Value = x(i)
Next
End With
End Sub
I need a looping structure that checks a range of cells, then if the cell and a cell that is in the range equal each other then the font should turn red. My problem is that my do until loop won't get entered. This is what I have right now.
`
Dim finalrow As Long
finalrow = Worksheets("Redundancy").Cells(Worksheets("Redundancy").Rows.Count, "D").End(xlUp).Row
Dim z As Long
Dim w As Long
Dim r As Long
w = 2
r = 0
For z = 2 To finalrow
If Range("L" & z) = Range("L" & z + 1) & Range("J" & z) <> Range("J" & z + 1) Then
Do Until Range("L" & z) = Range("L" & z + 1) & Range("J" & z) <> Range("J" & z + 1)
If Cells(w, 4) = Cells(z + 1, 4 + r) Then
Cells(w, 1).Font.ColorIndex = 3
Cells(z + 1, 1).Font.ColorIndex = 3
If r = 4 Then
w = w + 1
End If
End If
r = r + 1
Loop
End If
Next z
`
I changed it to this, but it exits the loop all together right when it is about to enter the do while loop.
`
For z = 2 To finalrow
Do While (Range("L" & z) = Range("L" & z + 1) And Range("J" & z) <> Range("J" & z + 1))
If Cells(w, 4) = Cells(z + 1, 4 + r) Then
Cells(w, 1).Font.ColorIndex = 3
Cells(z + 1, 1).Font.ColorIndex = 3
If r = 4 Then
w = w + 1
End If
End If
r = r + 1
Loop
Next z
`
If you do this;
Range("L" & z) = Range("L" & z + 1) and Range("J" & z) <> Range("J" & z + 1)
you are comparing Range objects. What you instead want to do is to compare the values in those range objects. So use this instead;
Range("L" & z).value = Range("L" & z + 1).value and Range("J" & z).value <> Range("J" & z + 1).value
However when you use the cells(row,column) you don't have this problem.
I am curious though, was it not possible to use conditional formatting instead?
Use the 'and' operator instead of '&'.
I am trying to make this code running:
Dim myrange As String
For i = LBound(lOffsets) To UBound(lOffsets)
myrange = "R" & CStr(2 * i + 4) & ":S" & CStr(2 * i + 5)
Set rangeT = Worksheets("ChartBuilder").Range(myrange)
Charts("overview").SeriesCollection.Add _
Source:=rangeT
Next i
But I always get: Run-time error '9': Subscript out of range. It seams that Range() object doesn't want to accept myrange String as an input. The code works perfectly though if I use Range("R4:S5") for example ...
Dim myrange As String
For i = LBound(lOffsets) To UBound(lOffsets)
myrange = "R" & CStr(2 * i + 4) & ":R" & CStr(2 * i + 5) & "," & "S" & CStr(2 * i + 4) & ":S" & CStr(2 * i + 5)
Set rangeT = Worksheets("ChartBuilder").Range(myrange)
ActiveSheet.ChartObjects("overview").Chart.SeriesCollection.Add _
Source:=rangeT
Next i
it works like this.
I am trying to get my code to insert a formula into an array based on how many data points there are in my data set. The code below almost works but after the first iteration of X is complete it does not insert the formula into the all the rows in the columns.
Worksheets(" Branded").Range("C3").Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
For Y = 1 To Column_Limit1 - 1
Range("C3").Offset(0, Y).Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
For X = 1 To Row_Limit1 - 1
Range("C3").Offset(X, 0).Formula = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
Next X
Next Y
Change to this:
With Worksheets(" Branded")
.Range(.Cells(3, 3), .Cells(Row_Limit1 + 2, Column_Limit1 + 2)).FormulaR1C1 = "=COUNTIFS(" & r.Address(ReferenceStyle:=xlR1C1) & ",RC2, " & r2.Address(ReferenceStyle:=xlR1C1) & ",R2C)"
.Range(.Cells(Row_Limit1 + 3, 3), .Cells(Row_Limit1 + 3, Column_Limit1 + 2)).FormulaR1C1 = "=SUM(R3C:R[-1]C)"
End With
When using R1C1 there is no need of a loop.
I have some code that searches a column of values in sheet(3) in the format of a string "value1 - value 2"
value2 is the first value in a column in sheet(2) and value1 is a value in the same column, in a cell further down the sheet.
The setup I have is:
In sheet(1), cells C2:C6 have values a-e respectively
In sheet(2), cell C1 has value "yes" and cells C2:C6 have values 1-5
respectively
In sheet(3), cell A2 has the value "4 - yes"
So the code should countif a column in sheet2 with the first value being yes and look for cells with the value 4, and put the result in cell B2 on sheet(3)
What it actually does is find the yes column (column C) and search the same column on sheet(1) (so the message boxes show letters rather than numbers).
Is there a way I can more precisely specify the sheet the countif function uses?
I'm using Excel 2000 on Windows 7
Private Sub test_click()
scenario_count = 6
Dim i As Integer
i = 1
Sheets(2).Select
For j = 2 To 24
If Sheets(2).Cells(1, j).Value = Right(Sheets(3).Cells(i + 1, 1).Value, Len(Sheets(3).Cells(i + 1, 1).Value) - InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 1) Then
MsgBox ("number of scenarios is " & scenario_count)
MsgBox ("value searching for is " & "'" & Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2) & "'")
MsgBox ("Range searched is " & Range(Cells(2, j), Cells(scenario_count, j)).Address & " in " & ActiveSheet.Name)
MsgBox ("Number of occurrences " & Sheets(2).Application.WorksheetFunction.CountIf(Range(Cells(2, j), Cells(scenario_count, j)), Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2)))
Sheets(2).Select
Sheets(3).Cells(i + 1, 2).Value = Sheets(2).Application.WorksheetFunction.CountIf(Range(Cells(2, j), Cells(scenario_count, j)), Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2))
For Each c In Range(Cells(2, j), Cells(scenario_count, j))
MsgBox ("comparing " & c.Address & " " & c.Value & " with " & Left(Sheets(3).Cells(i + 1, 1).Value, InStrRev(Sheets(3).Cells(i + 1, 1).Value, "-") - 2))
Next c
GoTo endofif2
End If
Next
endofif2:
End Sub
Where you have 'WorksheetFunction.CountIf(Range(Cells(2, j)', simply insert the sheet before the range reference, like so:
Sheets(2).Range(Sheets(2).Cells(2, j), Sheets(2).Cells(scenario_count, j))
EDIT full formula which references the sheet for both the Cells and the Range functions blatently taken from #Rory's comment.