I'm trying to send data between worksheets. When I run the code below, I get a 13 mismatch error on the IncidentReport variable. On the source sheet these ranges are numerical but one cell is a date (so number, date, number). I have tried to declare the IncidentReport as a string, integer, and long but I keep getting this error. What is the correct variable for this?
Private Sub Update_Click()
Dim AgentName As String
Dim IncidentReport As Long
Dim MyData As Workbook
'Names variable
Worksheets("sheet1").Select
AgentName = Range("C2")
IncidentReport = Range("D1:F1")
'Opens master workbook
Set MyData = Workbooks.Open("C:\Users\ashley.graham\Field_AgentFolder\Incident Reports\Test folder\Incident reports 2018.xlsx")
'Calls sheet and selects cell
Worksheets("Incident Reports").Select
Worksheets("Incident Reports").Range("a1").Select
'finds next blank row for data entry
RowCount = Worksheets("Incident Reports").Range ("A1").CurrentRegion.Rows.Count
With Worksheets("Incident Reports").Range("A1")
.Offset(RowCount, 0) = IncidentReport
.Offset(RowCount, 1) = AgentName
End With
MyData.Save
End Sub
I think this is what you're trying to do:
Private Sub Update_Click()
Dim AgentName As String
Dim IncidentReport As Range
Dim MyData As Workbook
Dim LastRow As Long
With ThisWorkbook.Worksheets("Sheet1")
AgentName = .Range("C2")
Set IncidentReport = .Range("D1:F1")
End With
Set MyData = Workbooks.Open("C:\Users\ashley.graham\Field_AgentFolder\Incident Reports\Test folder\Incident reports 2018.xlsx")
With MyData.Worksheets("Incident Reports")
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(LastRow, 1) = IncidentReport.Cells(1) 'This will put the first cell value here as don't know what you want.
.Cells(LastRow, 2) = AgentName
End With
MyData.Save
End Sub
Related
I have two sheets in my excel PullData and AllStocks. I would like to copy data from PullData column A and paste the values reverse order into other sheet AllStocks.
Currently, I am using OFFSET function to perform it. But I see a performance issue while running large data set using this method. Is there any better way I can perform this task ?
My CUrrent Code :
Sub GetData()
Dim Main As Worksheet
Dim PullData As Worksheet
Dim AllStocks As Worksheet
Dim i,m As Integer
Set RawImport = Workbooks("vwap.xlsm").Sheets("RawImport")
Set PullData = Workbooks("vwap.xlsm").Sheets("PullData")
m = PullData.Cells(Rows.Count, "A").End(xlUp).Row
For i = 3 To m
AllStocks.Range("A2:A" & i).Formula = "=OFFSET(PullData!$A$" & m & ",-(ROW(PullData!A1)-1),0)"
Next i
End Sub
no loop code:
Option Explicit
Sub GetData()
Dim pullDataVals As Variant
With Workbooks("vwap.xlsm")
With .Sheets("PullData")
pullDataVals = Split(StrReverse(Join(Application.Transpose(.Range("A3", .Cells(.Rows.Count, "A").End(xlUp)).Value), ",")), ",")
End With
.Sheets("RawImport").Range("A2").Resize(UBound(pullDataVals) + 1).Value = Application.Transpose(pullDataVals)
End With
End Sub
just check your sheets names: in your question you're speaking about "PullData and AllStocks" but in your code some RawImport sheet is featuring...
or, in a super compressed style:
Sub GetData()
With Workbooks("vwap.xlsm").Sheets("PullData")
With .Range("A3", .Cells(.Rows.Count, "A").End(xlUp))
.Parent.Parent.Sheets("RawImport").Range("A2").Resize(.Rows.Count).Value = Application.Transpose(Split(StrReverse(Join(Application.Transpose(.Value), ",")), ","))
End With
End With
End Sub
should your data in PullData be a more than one character string or more than one digit number, to prevent what Gary's Student remarked, you could use ArrayList object and its Reverse method:
Sub GetData()
Dim arr As Object
Dim cell As Range
Set arr = CreateObject("System.Collections.Arraylist")
With Workbooks("vwap.xlsm")
With .Sheets("PullData")
For Each cell In .Range("A3", .Cells(.Rows.Count, "A").End(xlUp))
arr.Add cell.Value
Next
End With
arr.Reverse
.Sheets("RawImport").Range("A2").Resize(arr.Count) = Application.Transpose(arr.toarray)
End With
End Sub
This solution applies the INDEX formula to a temporary Name.
Sub Range_ReverseOrder()
Const kFml As String = "=INDEX(_Src,#RowsSrc+#RowTrg-ROW(),1)"
Dim nmSrc As Name, rgTrg As Range
Dim lRows As Long, sFml As String
Rem Set Objects
With Workbooks("vwap.xlsm")
lRows = .Worksheets("PullData").Cells(Rows.Count, 1).End(xlUp).Row
Set nmSrc = .Names.Add(Name:="_Src", _
RefersTo:=.Worksheets("PullData").Cells(2, 1).Resize(-1 + lRows, 1))
.Names("_Src").Comment = "Range_ReverseOrder"
Set rgTrg = .Worksheets("RawImport").Cells(2, 1).Resize(-1 + lRows, 1)
End With
Rem Set Formula
sFml = kFml
sFml = Replace(sFml, "#RowsSrc", nmSrc.RefersToRange.Rows.Count)
sFml = Replace(sFml, "#RowTrg", rgTrg.Row)
Rem Apply Formula
With rgTrg
.Offset(-1).Resize(1).Value = "Reverse.Order"
.Formula = sFml
.Value2 = .Value2
End With
Rem Delete Temporary Name
nmSrc.Delete
End Sub
I would like to copy the data from one workbook to another.
My source workbook has some comments listed in each row. When I use my code to copy, it does not copy the comments accordingly. Could any one help, how I could copy from one workbook to another with the comment field ? my comments are in Column P.
Sub Extract()
Dim DestinationWB As Workbook
Dim OriginWB As Workbook
Dim path1 As String
Dim FileWithPath As String
Dim lastRow As Long, i As Long, LastCol As Long
Dim TheHeader As String
Dim cell As Range
Set DestinationWB = ThisWorkbook
path1 = DestinationWB.Path
FileWithPath = path1 & "\Downloads\CTT.xlsx"
Set OriginWB = Workbooks.Open(filename:=FileWithPath)
lastRow = OriginWB.Worksheets("Report").Cells(Rows.count, 1).End(xlUp).Row
LastCol = OriginWB.Worksheets("Report").Cells(22, Columns.count).End(xlToLeft).Column
For i = 1 To LastCol
'get the name of the field (names are in row 22)
TheHeader = OriginWB.Worksheets("Report").Cells(22, i).Value
With DestinationWB.Worksheets("CTT").Range("A4:P4")
'Find the name of the field (TheHeader) in the destination (in row 4)
Set cell = .Find(TheHeader, LookIn:=xlValues)
End With
If Not cell Is Nothing Then
OriginWB.Worksheets("Report").Range(Cells(23, i), Cells(lastRow, i)).Copy Destination:=DestinationWB.Worksheets("CTT").Cells(5, cell.Column)
Else
'handle the error
End If
Next i
OriginWB.Close SaveChanges:=False
End Sub
I refactored your code correcting the unqualified references and printing the Source and Destination range addresses to the Immediate window. This should give you an idea of what is going on.
Sub Extract()
Dim DestinationWB As Workbook
Dim OriginWB As Workbook
Dim FileWithPath As String, path1 As String, TheHeader As String
Dim lastRow As Long, col As Long
Dim cell As Range, Source As Range
Set DestinationWB = ThisWorkbook
path1 = DestinationWB.Path
FileWithPath = path1 & "\Downloads\CTT.xlsx"
Set OriginWB = Workbooks.Open(Filename:=FileWithPath)
With OriginWB.Worksheets("Report")
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For col = 1 To .Cells(22, .Columns.Count).End(xlToLeft).Column
'get the name of the field (names are in row 22)
TheHeader = OriginWB.Worksheets("Report").Cells(22, col).Value
With DestinationWB.Worksheets("CTT").Range("A4:P4")
'Find the name of the field (TheHeader) in the destination (in row 4)
Set cell = .Find(TheHeader, LookIn:=xlValues)
End With
If Not cell Is Nothing Then
Set Source = .Range(.Cells(23, col), .Cells(lastRow, col))
Source.Copy Destination:=cell.Offset(1)
Debug.Print Source.Address(External:=True), "Copied to ", cell.Offset(1).Address(External:=True)
Else
'handle the error
End If
Next
End With
OriginWB.Close SaveChanges:=False
End Sub
I'm trying to copy and paste data from one workbook into another. This function works when the range is static, but I cannot get it to be dynamic. I know the endxldown function is for this, but how would I work it into my code:
Private Sub CommandButton21_Click()
Dim itemName As String
Dim itemPrice As Single
Dim myData As Workbook
Worksheets("Sheet1").Select
itemName = Range("A2")
Worksheets("Sheet1").Select
itemPrice = Range("B2")
Set myData = Workbooks.Open("C:\Users\Iraj.Masud\Desktop\testing\Master.xlsm")
Worksheets("Sheet1").Select
Worksheets("Sheet1").Range("A1").Select
RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
With Worksheets("Sheet1").Range("A1")
.Offset(RowCount, 0) = itemName
.Offset(RowCount, 1) = itemPrice
End With
myData.Save
End Sub
My edits are highlighted in the image below. When I ran the macro, I received an object variable or with block variable not set.
Code with Error
I'd guess:
With Worksheets("Sheet1")
With .Range("A" & .Rows.Count).end(xlUp)
.Offset(1, 0) = itemName
.Offset(1, 1) = itemPrice
End With
End With
try this, (I cleaned up your code), see also the instruction that sets a value to rowcount
Private Sub CommandButton21_Click()
Dim rowcount As Long
Dim myData As Workbook
With Worksheets("Sheet1")
.Range("A2:B" & .Cells(.Rows.Count, 1).End(xlUp).Row).Copy
End With
Set myData = Workbooks.Open("C:\Users\Iraj.Masud\Desktop\testing\Master.xlsm")
With myData.Worksheets("Sheet1")
rowcount = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(rowcount, 1).Paste
End With
myData.Save
End Sub
Use End(xlUp) from the bottom to capture the last row, this is better than using xlDown because the latter will capture the first empty row, not the last row.
Removing all the unnecessary Select stuff and using arrays, your code can be reduced to this:
Private Sub CommandButton21_Click()
With Workbooks.Open("C:\Users\Iraj.Masud\Desktop\testing\Master.xlsm").Sheets("Sheet1")
.Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(1, 2).Value = _
ThisWorkbook.Sheets("Sheet1").Range("A2:B2").Value2
.Parent.Close True
End With
End Sub
Note that all references are explicit, there is no implicit reference to whichever workbook or worksheet is active. This usually results in safer code.
EDIT
If you want to copy all A:B data from the source, use this:
Private Sub CommandButton21_Click()
Dim src as Range
Set src = ThisWorkbook.Sheets("Sheet1").Range("A2", ThisWorkbook.Sheets("Sheet1").Range("B999999").End(xlUp)
With Workbooks.Open("C:\Users\Iraj.Masud\Desktop\testing\Master.xlsm").Sheets("Sheet1")
.Cells(.Rows.Count, "A").End(xlUp).Offset(1) _
.Resize(src.Rows.Count, src.Columns.Count).Value = src.Value2
.Parent.Close True
End With
End Sub
Private Sub CommandButton21_Click()
Dim itemName As String
Dim itemPrice As Single
Dim wbData As Workbook
Dim wsData As Worksheet
Dim newData As Range
With ThisWorkbook.Worksheets("Sheet1") '<~defines that we are working on sheet1
itemName = .Range("A2") '<~gets the itemname
itemPrice = .Range("B2") '<~gets the itemprice
End With
Set wbData = Workbooks.Open("C:\Users\Iraj.Masud\Desktop\testing\Master.xlsm") '<~opens then workbook
Set wsData = wbData.Worksheets("Sheet1") '<~sets the worksheet
Set newData = wsData.Range("A1048576").End(xlUp).Offset(1, 0) '<~locate last cell and offset 1 row below
With newData '<~defines that we are working with the cell below the last non-blank cell
.Offset(0, 0).Value = itemName '<~passes the item name
.Offset(0, 1).Value = itemPrice '<~passes the item price
End With
Set newData = Nothing '<~post procedure cleaning
Set wsData = Nothing '<~post procedure cleaning
wbData.Save '<~save
wbData.Close
End Sub
This might be a chunky code. But IMO, this is flexible in case you want to change some part/s of the code.
I am creating a VBA application that will find the text that I have entered in a certain range (Should be dynamic, in order for me to input more in the future). With that the entered texts in the range will look for the words in another sheet column:
Example:
And it will look for the words inputted in another sheet.
Dim Main as Worksheet
Set Main = Sheets("Sheet1")
Dim Raw2 as Worksheet
Set Raw2 = Sheets("Sheet2")
LookFor = Main.Range(D8:100)
Fruits = Raw2.Range("G" & Raw2.Rows.Count).End(xlUp).row
For e = lastRow To 2 Step -1
value = Raw2.Cells(e, 7).value
If Instr(value, LookFor) = 0 _
Then
Raw2.Rows(e).Delete
Honestly I am not sure how to proceed. And the mentioned code is just experiment. Desired output is to delete anything in sheet2 except for the rows that contain the words that I have inputted in the "Look for the words". Hope you can help me. Thank you.
This should do the trick :
Sub Sevpoint()
Dim Main As Worksheet
Set Main = Sheets("Sheet1")
Dim Raw2 As Worksheet
Set Raw2 = Sheets("Sheet2")
Dim LooKFoR() As Variant
Dim LastRow As Double
Dim i As Double
Dim j As Double
Dim ValRow As String
Dim DelRow As Boolean
LooKFoR = Main.Range(Main.Range("G8"), Main.Range("G" & Main.Rows.Count).End(xlUp)).Value
LastRow = Raw2.Range("G" & Raw2.Rows.Count).End(xlUp).Row
For i = LastRow To 2 Step -1
ValRow = Raw2.Cells(i, 7).Value
DelRow = True
'MsgBox UBound(LooKFoR, 1)
For j = LBound(LooKFoR, 1) To UBound(LooKFoR, 1)
If LCase(ValRow)<>LCase(LooKFoR(j, 1)) Then
Else
DelRow = False
Exit For
End If
Next j
If DelRow Then Raw2.Rows(i).Delete
Next i
End Sub
I am new here. I have been trying to solve this problem but to no avail.
My objectives are:
1) Need to find matching cell in the Installment workbook and paste the data into the next empty cell on the same row as the matching cell. The matching cell will come from Column E.
Private Sub CommandButton1_Click()
Dim itemNumber As String
Dim itemAmount As Single
Dim myData As Workbook
Worksheets("sheet1").Select
itemNumber = Range("B6")
Worksheets("sheet1").Select
itemAmount = Range("H16")
Set myData = Workbooks.Open("C:\Mydata\Installment.xlsx")
Worksheets("sheet1").Select
Worksheets("sheet1").Range("E1").Select
For Each cell In Range(“E2:E10000”)
IF cell.Value = itemNumber Then
Cellvalue = ActiveCell.address
Next
End IF
ColumnCount = Worksheets("sheet1").Range("Cellvalue").CurrentRegion.Columns.Count
With Worksheets("sheet1").Range("Cellvalue")
.Offset(0, ColumnCount) = itemNumber
.End With
myData.Save
Workbooks("Installment.xlsx").Close
End Sub
If I'm understanding you correctly, this should work. I've made a few "tweaks" to your code, primarily removing the "select" statements, which are usually undesirable and unnecessary.
Private Sub CommandButton1_Click()
Dim itemNumber As String
Dim itemAmount As Single
Dim myData As Workbook
Dim Wk As Worksheet
Dim Cel As Range
With ThisWorkbook.Worksheets("sheet1")
itemNumber = .Range("B6").Value
itemAmount = .Range("H16").Value
End With
Set myData = Workbooks.Open("C:\Mydata\Installment.xlsx")
Set Wk = myData.Worksheets("sheet1")
For Each Cel In Wk.Range("E2:E10000")
If Cel.Value = itemNumber Then
Cel.Value = Cel.Address
' get next blank cell in row
Wk.Cells(Cel.Row, Wk.Columns.Count).End(xlToLeft).Offset(, 1).Value = itemNumber
End If
Next
myData.Close True 'save & close
End Sub