Pointing to a cell address given in string format - vba

I have cell addresses given in an excel sheet and wanted to print a specific formula in other excel sheet referring to the cell addresses given in the previous sheet. Please suggest vba function for this. The cell addresses are in string format so corresponding row no. and col. no need to be extracted from the string. Is there an existing function for this in vba?

I'm not sure I understand correctly, but the Range method accepts string input. For your example, this would work:
Worksheets("Sheet2").Range(Worksheets("Sheet1").Range("A1").Value)=Worksheets("Sheet1").Range("A2").Value
Same code, a bit easier on the eyes:
Dim strLocation As String
strLocation = Worksheets("Sheet1").Range("A1").Value
Worksheets("Sheet2").Range(strLocation) = Worksheets("Sheet1").Range("A2").Value
Is this what you are looking to do? Do you need more help understanding it?

Below code will check for last row in sheet1 column A and try to get values present in odd rows in column A and print the result in even row numbers
Sub testt1()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("sheet1")
Set ws2 = Worksheets("sheet2")
Dim lastRow As Long
lastRow = ws1.Range("A" & Rows.Count).End(xlUp).Row
On Error Resume Next
For i = 1 To lastRow
t1 = ws1.Cells(i, 1).Value
t2 = ws2.Range(t1).Value
ws1.Cells(i + 1, 1).Value = t2
i = i + 1
Next
End Sub

Related

Auto Fill Next Column VBA

I have some code here that works, but I'm having some troubles getting it to work multiple times in a row. What it does is it fills the first empty column with a formula from a different sheet.
Dim source As Worksheet
Dim destination As Worksheet
Dim EmptyColumn As Long
Dim LastRow As Long
Set source = Sheets("vlookup")
Set destination = Sheets("COMMIT")
LastColumn = destination.Cells(1,destination.Columns.Count).End(xlToLeft).Column
LastRow = Worksheets("COMMIT").Range("A:A").Rows.Count
If IsEmpty(destination.Range("A2")) = False Then
EmptyColumn = LastColumn + 1
destination.Cells(3, EmptyColumn).Formula = "=INDEX(PORT!$S$5:$S$4000,MATCH(COMMIT!$G3,PORT!$G$5:$G$4000,0))"
LastRow = ActiveSheet.UsedRange.Rows.Count
Range("AL3").AutoFill destination:=Range("AL3:AL" & LastRow) **'This is where I'm having issues'**
End If
I would like it to keep putting the index formula into the next empty column, but I only know how to put it into a range that I have set and will not move to the next column when I run this macro again.
Any thoughts on how I could achieve this?
Thanks to Scott for helping me out with this!
Now I have another question that has popped up in my code.
Set cellSource = Worksheets("COMMIT").Range(Cells(1, LastColumn).Address)
Set cellTarget = Worksheets("COMMIT").Range(Cells(1, LastColumn), Cells(1, EmptyColumn))
If detntn.Range("A2") <> "" Then
cellSource.AutoFill destination:=cellTarget, Type:=xlFillDefault
End If
Now I'm trying to copy a formula from the "Commit" worksheet and paste it in the first cell of the first empty column. For some reason this code is not working. Wondering if anyone can sniff out as to why. Thanks in advance for your help!
Part of the problem is you have used a reserved word for a variable. Look at the AutoFill function, It uses Destination.
Then we can use the Cells() instead of Range to refer to the last column.
Dim source As Worksheet
Dim detntn As Worksheet
Dim EmptyColumn As Long
Dim LastRow As Long
Set source = Sheets("vlookup")
Set detntn = Sheets("COMMIT")
LastColumn = detntn.Cells(1, detntn.Columns.Count).End(xlToLeft).Column
LastRow = Worksheets("COMMIT").Range("A:A").Rows.Count
If detntn.Range("A2")<>"" Then
EmptyColumn = LastColumn + 1
detntn.Cells(3, EmptyColumn).Formula = "=INDEX(PORT!$S$5:$S$4000,MATCH(COMMIT!$G3,PORT!$G$5:$G$4000,0))"
LastRow = ActiveSheet.UsedRange.Rows.Count
detntn.Cells(3, EmptyColumn).AutoFill destination:=detntn.Range(detntn.Cells(3, EmptyColumn), detntn.Cells(LastRow, EmptyColumn))
End If

Copy and Paste a Column only if data is present above it (Such as a name) in Excel VBA

I have been using StackOverflow for a while now and just love this community. I know I can get an answer for any problem hopefully.
Ok so here is my issue, I have been performing a "Frankenstein" of a script from several posts on this site. I want to copy and paste a column of an Array Formula beneath specific headers until there are no more headers. For example, in the row F5 through W5, if there is a name, I want to copy the range that has an array formula beneath it, say in the range F156:F323, and paste that formula in the name under the G Column, H Column, so on until there are no more names between that range...
Below is my attempt to solve it but I keep getting errors
Dim lastCol As Long
Dim i As Long
Dim ws As Worksheet
Dim Formula As Range
Set ws = Sheets("Main")
lastCol = ws.Range("F" & Columns.Count).End(xlRight).Column
Set Formula = Sheets("Main").Range("F156:F323")
With ws
For i = 6 To lastCol
If len(trim.range("F" & i).Value)) <> 0 then _
.Range(i & 156).formulaarray = 'my formula here'
Next i
End With
Post any questions you may have and thanks!
You are flipping columns and rows in many instances.
Use the Range Object Cells instead of Range. It allows using column references in numbers instead of letters.
Assign the formula directly.
Dim lastCol As Long
Dim i As Long
Dim ws As Worksheet
Dim Frmla As Range
Set ws = Sheets("Main")
lastCol = ws.Cells(5, ws.Columns.Count).End(xlToLeft).Column
Set Frmla = ws.Range("F156:F323")
With ws
For i = 6 To lastCol
If Len(Trim(.Cells(5, i).Value)) <> 0 Then
.Range(.Cells(156, i), .Cells(323, i)).FormulaR1C1 = Frmla.FormulaR1C1
End If
Next i
End With

if else statement at copying and pasting a cell value

I have the following code which will copy/paste some columns from "data" worksheet and pastes to the next empty column in to the column that i specify in the mastersheet called "KomKo".
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("data")
Set pasteSheet = Worksheets("KoMKo")
lRow = copySheet.Cells(copySheet.Rows.Count, 1).End(xlUp).Row
With copySheet.Range("BX2:BX" & lRow)
pasteSheet.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(.Rows.Count, .Columns.Count) = .Value
End With
Now i would like to add an if condition for another column; which should say "if column U in Worksheet "data" has cell value "8636" then these values should be pasted to Column H in Worksheet "KomKo"(pastesheet); to the next row as i used the code above in the "with" part.
Else( If the value in Column H is not 8636) then it should paste the value inside this column to Column G at Worksheet "KomKo"(pastesheet) with same preferences as above again.
How can i do this ?
So, I've come up with a suggestion below using an if-then within a loop. I think it's close to what you want...
Sub try6()
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim x As Range
Set ws = Worksheets("data")
Set ws2 = Worksheets("KomKo")
For Each x In ws.Range("C1:C100")
If x.Value = 8636 Then
ws2.Range("H:H").Value = ws.Cells(Rows.Count, "A").Value
ElseIf x <> 8636 Then
ws2.Range("G:G").Value = ws.Range(Rows.Count, "B").Value
End If
Next x
End Sub
Testing it, it took a while to execute. I'd say, set a dynamic range at something like A10000 and copy it directly without needing to necessarily test for whether there is a value in the range being copied.
You can also use the Select method for the purpose and copy the selection - from personal experience, I've had mixed success with it and I've seen people advise against using it here.
These are my .02, hope it helps! Cheers.

How to return value from VLOOKUP into a cell?

I have the following piece of code for Vlookup. The function works fine but the found out value aint getting displayed in the cell. However if i had a used Msgbox function the found out value is shown. The question is doesnt VLOOKUP result be captured in a cell?
Sub Example_of_Vlookup()
Dim lookFor As Range
Dim rng As Range
Dim col As Integer
Dim found As String
Dim lastrowrange As Long
Dim area As Range
lastrowrange = [A65536].End(xlUp).Row
Set lookFor = Sheets("Sheet2").Range("b2")
Set rng = Sheets("Sheet2").Columns("t:u")
Set taxRange = Range("f2", Cells(lastrowrange, 22))
col = 2
On Error Resume Next
For i = 1 To lastrowrange
found = Application.VLookup("B2", "T1:U4", 2, True)
If IsError(found) Then
MsgBox lookFor & " not found"
Else
area.Cells(i, 2).Value = found
End If
Next i
On Error GoTo 0
End Sub
You did not set the range "area" equal to anything, so this line won't show your answer properly:
area.Cells(i, 2).Value = found
Change area.Cells(i,2).value to sheets("Sheet2").Cells(i,2).value or wherever you want your answer to show. Or, set area equal to something if you want to use area.cells.
Idea is simple - I have country names in Column B.Inention is to pull out the Area under which country belongs - My look up values are in column S(country) and T(area) and display the result in column F – Sayanth Sasidharan 25 mins ago
If my understanding is correct as per your explanation then you do not need to use a loop. Let Excel do the Dirty Work ;) You will end up with far less code.
Let's say your sheet looks like this
Logic:
Find the last row of Col B
Insert the Vlookup formula in F1:F & LastRow in one go
Convert them to values.
Code:
Option Explicit
Sub Example_of_Vlookup()
Dim ws As Worksheet
Dim lRow As Long
Set ws = ThisWorkbook.Sheets("Sheet2")
With ws
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
'~~> =IF(ISERROR(VLOOKUP(B1,S:T,2,0)),"",VLOOKUP(B1,S:T,2,0))
.Range("F1:F" & lRow).Formula = _
"=IF(ISERROR(VLOOKUP(RC[-4],C[13]:C[14],2,0)),"""",VLOOKUP(RC[-4],C[13]:C[14],2,0))"
.Range("F1:F" & lRow).Value = .Range("F1:F" & lRow).Value
End With
End Sub
Result:

Excel VBA on Range Error - Quick Fix?

The below code gives an error but when I change the range to .Range(A2:A9999) it works...
But it's ugly. Is there way to grab all the data in column(A) from A2 and down? and copy that into B2?
Sheets("AA").Range("A2:A").Value = Sheets("BB").Range("B2:B").Value
To have it in one line you can do it as follows:
Sheets("AA").Range("A2", Cells(Rows.Count, "A")).Copy Sheets("BB").Range("B2")
By the way, it copies everything, not only values but also formatting. To run it your sheet AA should be active one.
Sub CopyValues()
Dim rValues As Range
With Sheet1
Set rValues = .Range("A2", .Cells(.Rows.Count, 1).End(xlUp))
End With
rValues.Offset(, 1).Value = rValues.Value
End Sub
By finding the last populated cell in column A, you can be more precise in what you are copying.
This copies the contents of column A on the src sheet to column B of the tgt sheet:
Sub CopyOneColumnToAnother()
Dim wb As Workbook
Dim src As Worksheet
Dim tgt As Worksheet
Dim lastRow As Long
Set wb = ThisWorkbook
Set src = wb.Sheets("Sheet5")
Set tgt = wb.Sheets("Sheet6")
lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
tgt.Range("B2:B" & lastRow).Value = src.Range("A2:A" & lastRow).Value
End Sub
The reason you are getting the error is because your ranges don't have valid end points. Range ([firstcell]:[lastcell]) is the correct syntax, so you need to add a row for the lastcell in both your source and target ranges.
Note that even if your code runs, it won't do want you want. You are setting column A to equal column B. You need to flip the left and right-hand sides of your statement.
Why not just copy the whole column and re-insert whatever was in B1:
Range("A:A").Copy Columns("B:B")
Range("B1").Value = "Whatever"
In more detail:
Dim previous 'As Variant (or whatever it is)
previous = Worksheets("BB").Range("B1").Value
Worksheets("AA").Range("A:A").Copy Worksheets("BB").Columns("B:B")
Worksheets("BB").Range("B1").Value = previous
Unless there is data further down in column B which you wish to keep (you haven't stated).