From excel to specific cells in a table in word - excel

I've seen some questions answered about creating tables in word from excel but they don't quite have what I'm looking for. I have an excel sheet that has the details on equipment (company #, serial #, manufacturer, description, and model #). This file currently has 17114 rows of equipment data. I have a word doc with four columns (quantity, company #, part #, description).
Right now on excel I have a button to open up the word doc and another that brings up a userform. The user form has a combo box and a text bot. The combo box chooses what column in excel to search in. The text box is what the person is looking for. The code for this is below
Dim myLastRow As Long
Dim myResult As Long
Dim myTableRange As Range
myLastRow = Cells(Rows.Count, 1).End(xlUp).Row
If ComboBox1.Value = "Serial" Then
Set myTableRange = Range("B1:B" & myLastRow)
myResult = Application.Match(TextBox1.Value, myTableRange, 0) 'Returns row number only
Range("B" & myVLookupResult).Activate
ElseIf ComboBox1.Value = "MII" Then
Set myTableRange = Range("A1:A" & myLastRow)
myResult = Application.Match(TextBox1.Value, myTableRange, 0) 'Returns row number only
Range("A" & myResult).Activate
Else
MsgBox ("No Range Selected")
End If
Where "MII" is the company #. This code is placed on a command button. From here I want the macro to copy the data from myResult over to word. The cells to copy would be
Cells(myResult, 1)
to the second column in word;
Cells (myResult, 2)
to the third column in word; and
Cells(myResult, 3) & ", " & Cells(myResult, 4) & ", Model #" & Cells(myResult, 5)
to the 4th column in word. I am also looking for word to check where the first blank row is (after the headers) and insert these there. And if there are no blank rows before the footer (also part of the table) to add a row.
The default number of rows I can put the data is 16. With 13 rows for the header (header is part of the table). A total of 19 rows will create a second page but without any cells on the second page for data (only the header and footer). It isn't until 28 rows are made that cells for data start popping up on page 2.
My questions are how do I reference specific cells in a table in word? Can I use the same code for finding the first blank cell after the header as I would in excel? Would the code also be the same for adding rows to the table and counting the available rows to make sure I'm typing on the right page?
Right now all I have for the word side of the macro is calling the document up.
Dim objWord, objDoc As Object
Set objWord = GetObject(, "Word.Application")
objWord.Visible = True
I know I can use something similar as below but that doesn't specify where to put the data.
Sheets(1).Range(FirstCell, LastCell).Copy
objWord.Selection.Paste
objWord.Selection.TypeParagraph

I still haven't figured out how to add rows automatically. I keep getting run-time error '5991': Cannot access individual rows in this collection because the table has vertically merged cells. (Edit: I found out I didn't have the Microsoft Word Object Library reference clicked. After doing this other answers to this question worked.)
Since what I have done is still a decent time saver for me and might help other people trying to do the same thing I'm going to post what I have so far. Note: there's still some unused code in there from trying out stuff to see if it worked or not.
Dim myLastRow As Long
Dim myResult As Long
Dim myTableRange As Range
myLastRow = Cells(Rows.Count, 1).End(xlUp).Row
If ComboBox1.Value = "Serial" Then
Set myTableRange = Range("B1:B" & myLastRow)
myResult = Application.Match(TextBox1.Value, myTableRange, 0) 'Returns row number only
ElseIf ComboBox1.Value = "MII" Then
Set myTableRange = Range("A1:A" & myLastRow)
myResult = Application.Match(TextBox1.Value, myTableRange, 0) 'Returns row number only
Else
MsgBox ("No Range Selected")
End If
Dim objWord, objDoc As Object
Set objWord = GetObject(, "Word.Application")
objWord.Visible = True
Dim tableRow As Long
Dim rowCount As Long
Dim lastTableCell As Long
Dim i As Long
Dim cellEmpty As Boolean
'lastTableCell = 28 'Defualt input range is from cell 13 to 28
lastTableCell = 100
cellEmpty = True
findEmptyCell:
For i = 13 To lastTableCell
If objWord.ActiveDocument.Tables(1).Cell(i, Column:=1).Range.Text = Chr(13) & Chr(7) Then
tableRow = i
cellEmpty = True
GoTo rowFound
End If
allCellsFilled:
If cellEmpty = False Then
objWord.ActiveDocument.Tables.Item(1).Rows(i - 1).Select
Selection.InsertRowsBelow (i - 1)
cellEmpty = True
GoTo findEmptyCell
End If
Next i
rowFound:
On Error GoTo errorHappened
objWord.ActiveDocument.Tables(1).Cell(Row:=tableRow, Column:=1).Range.Text = "1"
objWord.ActiveDocument.Tables(1).Cell(Row:=tableRow, Column:=2).Range.Text = Cells(myResult, 1).Value
objWord.ActiveDocument.Tables(1).Cell(Row:=tableRow, Column:=3).Range.Text = Cells(myResult, 2).Value
objWord.ActiveDocument.Tables(1).Cell(Row:=tableRow, Column:=4).Range.Text = Cells(myResult, 3).Value & ", " & Cells(myResult, 4).Value & ", Model # " & Cells(myResult, 5).Value
GoTo endTheSub
errorHappened:
cellEmpty = False
GoTo allCellsFilled
endTheSub:
End Sub

Related

Compare two Excel workbooks for differences and copy/paste all the information from one to another

On some difficult issue I am bumped in. And i guess it quite out of my knowledge, and I hope it is even possible to solve on some way.
ISSUE:
Two different workbooks: I am having one workbook with 10 sheets inside, with many formulas, dropdowns, calculations etc., and it is main version of the document which has to be filled with information.
Second workbook, or better to say another similar version to this workbook is like obsolete versions of main wb, where might be possible that some cells/format, or even sheet is missing, but in general almost the same from its structure.
PROCESS:
Sometimes the customers are not having the newest version of excel workbook, but still some of the obsolete versions (they are forgetting to use the newest version), and they are filling those fields inside those older versions and sending them back. The problem is, our ERP Software cant read the obsolete versions, because it is so adjusted to read only the newest version of the document. Meaning, it has to be manually checked every time when the document is sent back and finding discrepancies and copy/paste them into newest version of the document, and then upload it into ERP...
RESULT:
I am looking for some solution, with VBA or even formulas how to check every other workbook against "newest" and if there are any discrepancy and differences just to copy/paste everything from old to new version. When I say "everything" it means, all the fields, sheets, calculations, 1:1.
Unfortunately I am not writing any code or formula, because this is for me super advanced.
On the pic below is one example of one sheet how it looks like. There are lot of columns, calcs and so on.
Explanation:
To clarify bit better the content: inside one workbook is usually 10 sheets. 8x of them are the same (gas chambers from 1-8) and depending on the customer wishes, they can populated from 1-8. Sometimes 1 sometimes 5.
And range is from A1:Q54, full of data, tables, calculations, dropdowns, infos..
One sheet (9th) is customer details and last one (10th) is just instruction sheet with infos and screenshots.
So optimal would be to have macro that is taking everything from older versions, compare it with new one, and populate data that it finds, or on already given workbook or on new one but with the same content. I dont know if that is something possible.
An example of how to scan various cell ranges in a workbook and collate those that have values into a table. Second stage would be to transfer those values to the new format template.
Option Explicit
Sub extractAll()
Dim myfile As String, wb As Workbook, ws As Worksheet
Dim n As Long, rng1 As Range, rng2 As Range, msg As String
' select workbook to scan
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Please select a file"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a file"
Exit Sub
End If
myfile = .SelectedItems(1)
End With
' collate data on sheet 1 and 2
Sheet1.Cells.Clear
Sheet1.Range("A1:E1") = Array("Sheet", "Addr", "Row", "Column", "Value")
Set rng1 = Sheet1.Range("A2")
Sheet2.Cells.Clear
Sheet2.Range("A1:D1") = Array("Addr", "Row", "Column", "Value")
Set rng2 = Sheet2.Range("A2")
' open workbook and scan worksheets
Set wb = Workbooks.Open(myfile, ReadOnly:=True)
For Each ws In wb.Sheets
If ws.Name Like "CH_recipe_#" Then
Call scanSheet(ws, rng1)
msg = msg & vbLf & ws.Name
ElseIf ws.Name = "Customer Details" Then
Call scanCustomer(ws, rng2)
msg = msg & vbLf & ws.Name
End If
Next
wb.Close savechanges:=False
MsgBox "Sheets scanned" & msg
End Sub
Sub scanSheet(ws As Worksheet, ByRef rng As Range)
Dim cell As Range, ar, s As String
Dim i As Long, n As Long
' old template layout
s = "D13,A15,C15,D15,E15,G15,J15,N15," ' process details
s = s & "E20:P24,E41:P41," ' Carrier, Gas 2-12, usage
s = s & "C45,D45,E45,G45,I45,K45,M45,N45,P45," ' exhaust line
s = s & "C48" ' notes and remarks
ar = Split(s, ",")
For i = 0 To UBound(ar)
For Each cell In ws.Range(ar(i))
If cell.Value <> "" Then
rng = ws.Name
rng.Offset(, 1) = cell.Address(0, 0)
rng.Offset(, 2) = cell.Row
rng.Offset(, 3) = cell.Column
rng.Offset(, 4) = cell.Value
Set rng = rng.Offset(1)
End If
Next
Next
Debug.Print ws.Name & " Done"
End Sub
Sub scanCustomer(ws As Worksheet, ByRef rng As Range)
Dim cell As Range, ar, s As String
Dim i As Long, n As Long
' old template layout
s = "B14:B25," ' contact details
s = s & "B28:B29," ' existing install
s = s & "B32:B35," ' hook up
s = s & "A38" ' remarks
ar = Split(s, ",")
For i = 0 To UBound(ar)
For Each cell In ws.Range(ar(i))
If cell.Value <> "" Then
rng = cell.Address(0, 0)
rng.Offset(, 1) = cell.Row
rng.Offset(, 2) = cell.Column
rng.Offset(, 3) = cell.Value
Set rng = rng.Offset(1)
End If
Next
Next
Debug.Print ws.Name & " Done"
End Sub

Is there a better way to compare against many strings with like?

I have a long list of words I need to compare against.
As an example fruit and vegetables that needs to be stored cold vs warmer:
Cold
strawberries
raspberries
lettuce
Warm(er)
cucumber
bell pepper
tomatoes
I have a sheet with products and need to loop it:
For Each cel In rng
If LCase(cel.Value) Like "*strawberries*" Or LCase(cel.Value) Like "*raspberries*" Or LCase(cel.Value) Like "*lettuce*" Then
msgbox "Cold"
ElseIf LCase(cel.Value) Like "*cucumber*" or LCase(cel.Value) Like "*bell pepper*" or LCase(cel.Value) Like "*tomato*" Then
msgbox "Warmer"
End If
Next cel
Is there any way I could this better? The syntax to test against all products will be very very long.
Could I somehow group/list them and make the syntax easier to maintain?
Example of the workbook:
To demonstrate what I meant with a wildcard match:
Sub Test()
Dim rng As Range, cl As Range
Dim Cold As Variant, Warm As Variant
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:A4")
Cold = Array("*strawberries*", "*raspberries*", "*lettuce*")
Warm = Array("*cucumber*", "*bell pepper*", "*tomatoes*")
With Application
For Each cl In rng
If .IsNumber(.Match(True, .IsNumber(.Match(Cold, cl, 0)), 0)) Then
'Or: If UBound(Filter(.IsNumber(.Match(Warm, cl, 0)), True)) = 0 Then
MsgBox "Cold"
ElseIf .IsNumber(.Match(True, .IsNumber(.Match(Warm, cl, 0)), 0)) Then
MsgBox "Warm"
End If
Next
End With
End Sub
Alternatively, you could use regular expressions with word-boundaries:
Sub Test()
Dim rng As Range, cl As Range
Dim Cold As String, Warm As String
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:A4")
Cold = "strawberries|raspberries|lettuce"
Warm = "cucumber|bell pepper|tomatoes"
With CreateObject("vbscript.regexp")
.IgnoreCase = True
For Each cl In rng
.Pattern = "\b" & Cold & "\b"
If .Test(cl) Then
MsgBox "Cold"
Else
.Pattern = "\b" & Warm & "\b"
If .Test(cl) Then MsgBox "Warm"
End If
Next
End With
End Sub
You can also, match both in any case and see if it's supposed to be a combination of warm and cold.
Here is a perfunctory system that would return the information you want from a list of produce.
Sub GetStorageInstruction()
' 187
Dim Veggie As Variant
Dim Storage As String
Dim Txt As String
Veggie = InputBox("Enter name of fruit or vegetable to store:", _
"Get storage instruction")
Veggie = Trim(Veggie)
If Len(Veggie) Then
Storage = StorageInstruction(Veggie)
If Len(Storage) Then
Txt = "Store " & Veggie & " at a " & Storage & " location."
Else
Txt = "Sorry, I couldn't find instructions on" & vbCr & _
"storage of """ & Veggie & """."
End If
MsgBox Txt, vbInformation, "Storage instructions"
End If
End Sub
Private Function StorageInstruction(ByVal Veggie As String) As String
' 187
' return vbNullString if not found
Dim ListRng As Range
Dim Fnd As Range ' found match
Dim C As Long ' column
' here items for "Cold" storage are in column A,
' items for "Cool" storage are in column B
Set ListRng = Range("A:B") ' adjust to suit
Set Fnd = ListRng.Find(Veggie, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False)
If Not Fnd Is Nothing Then
' the return result is taken from the caption row (row 1)
' of the column in which a match was found
StorageInstruction = Cells(1, Fnd.Column).Value
End If
End Function
"Ordinarily", you wouldn't work with an InputBox because it's too error prone (typos) but with a validation list or combo box that is based on the same lists. But for the moment, if you are concerned about not finding "Bell peppers" (plural), consider either listing "Bell pepper" as well or modify the search to LookAt:=xlPart.
To make the above code work for you immediately, just type "Cold" in A1, "Cool" in B1 and a list of produce under each header. The code will return the header from the column where the item was found.
I see that you have now added a view of your worksheet. That is a much better base. Instead of the produce name, list a number from your columns C or D (whichever is unique), in my columns A and B, and enter that number in the InputBox. Once you implement that system you can modify the returned answer by using the number to VLOOKUP the product name so that the description appears in the answer along with the number you entered.
As an afterthought, the best way for you would probably be to just select the row you are interested in, click a button (or keyboard shortcut) and have the storage instruction pop up. But the presumption here is that you should be able to attach VBA code to your workbook.
No. Correct. I solved that with search for asparagus then search again in the same string for potatoes. But I know there will be false matches. There is no way around it. Let's just say, if there is a handful of false matches is better than looking through the full sheet manually (30-40 000 rows). – Andreas 1 hour ago
Here is an example of what I recommend. Feel free to go with other answers. If there are multiple matches then fill the cell with "Cold/Warm" as mentioend in the code comments below. This way you can simply filter on these and fix them manually.
Basic Preparation to test this
Create a master sheet in the file which has the code. Let's call it MasterList. The reason why we are doing this is so that it is easier to maintain and when you are distributing the code file, the masterlist is easily available. You can do version control on the file so that everyone uses the current version. Let's say the MasterList looks like this.
Let's say the file (as shown in your image) is called MyData.xlsx and the data is in Sheet1. Feel free to change it in the code below. It looks like this
Code
Option Explicit
Sub Sample()
Dim wsThis As Worksheet
'~~> This is the hidden sheet which has your master list in the file
'~~> which has the code
Set wsThis = ThisWorkbook.Sheets("MasterList")
Dim lRow As Long
Dim MasterList As Variant
With wsThis
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
MasterList = .Range("A2:B" & lRow).Value2
End With
Dim wb As Workbook
Dim wsThat As Worksheet
'~~> Change this to the workbook where the data needs to be checked
Set wb = Workbooks.Open("C:\Users\Siddharth Rout\Desktop\MyData.xlsx")
'~~> Change this to the workseet where the data needs to be checked
Set wsThat = wb.Sheets("Sheet1")
Dim rngToProcess As Range
With wsThat
'~~> Find last row in Col E which has names
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
'~~> Identify your range
Set rngToProcess = .Range("E2:E" & lRow)
'~~> Insert a blank column for output
.Columns(6).Insert Shift:=xlToRight
End With
Dim SearchText As String
Dim aCell As Range, bCell As Range
Dim i As Long
'~~> Loop through the masterlist
For i = LBound(MasterList) To UBound(MasterList)
SearchText = MasterList(i, 1)
Set aCell = rngToProcess.Find(What:=SearchText, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
'~~> Get the Warm - Cold - Warm/Cold Status
aCell.Offset(, 1).Value = GetStatus(MasterList(i, 2), aCell.Offset(, 1).Value)
'~~> Search again for multiple occurences
Do
Set aCell = rngToProcess.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
aCell.Offset(, 1).Value = GetStatus(MasterList(i, 2), aCell.Offset(, 1).Value)
Else
Exit Do
End If
Loop
End If
Next i
End Sub
'~~> Common function to asign the values
'~~> If there are multiple matches then fill the cell with "Warm/Cold".
'~~> This way you can simply filter on these and fix them manually.
Private Function GetStatus(MasterStatus As Variant, CurrentStatus As String) As String
Dim newStatus As String
If MasterStatus = "Cold" Then
Select Case CurrentStatus
Case "Warm": newStatus = "Warm/Cold"
Case Else: newStatus = MasterStatus
End Select
ElseIf MasterStatus = "Warm" Then
Select Case CurrentStatus
Case "Cold": newStatus = "Warm/Cold"
Case Else: newStatus = MasterStatus
End Select
End If
GetStatus = newStatus
End Function
Output
When you run the above code you get the below output
Here is an alternative to my earlier answer. Install the code below in a standard code module and make some arrangement to call it, perhaps with a keyboard shortcut or even a button on the sheet. Then simply select an item (anywhere in the list, no particular column) and run the code. You don't need to enter anything.
Sub Storage_Instruction()
' 187
Const SKUClm As String = "D" ' change to point at the SKU column in 'Data'
Const DescClm As String = "E" ' change to point at the Description column in 'Data'
Const StgClm As String = "C" ' change to point at Storage Instruction column in WsList
Dim WsData As Worksheet
Dim WsList As Worksheet
Dim SKU As String ' SKU number from row R
Dim Desc As String ' Description from row R
Dim R As Long ' the selected row
Dim LookUpRng As Range ' in WsList
Dim Fnd As Range ' found match
Dim Storage As Variant
Dim Txt As String
Set WsData = Worksheets("Data") ' insert your data sheet's name here
Set WsList = Worksheets("Storage") ' change name to suit
With WsList
' my list has an extra column for 'Description'
' the storage instruction is in column C (=StgClm)
Set LookUpRng = .Range(.Cells(2, "A"), .Cells(.Rows.Count, StgClm).End(xlUp))
End With
If ActiveSheet Is WsData Then
R = Selection.Row
With WsData
SKU = .Cells(R, SKUClm).Value
Desc = .Cells(R, DescClm).Value
End With
Set Fnd = LookUpRng.Find(SKU, LookIn:=xlValues, Lookat:=xlWhole)
If Fnd Is Nothing Then
Txt = "Sorry, I couldn't find instructions for the" & vbCr & _
"storage of " & Desc & "(SKU " & SKU & ")."
Else
Storage = WsList.Cells(Fnd.Row, StgClm).Value
Txt = "Store " & Desc & " (SKU " & SKU & ") at " & String(2, vbCr) & _
String(8, Chr(32)) & UCase(Storage) & String(2, vbCr) & "temperature."
End If
End If
MsgBox Txt, vbInformation, "Storage instruction"
End Sub
For the setup you do need to specify the 3 constants at the top of the procedure and the names of the two worksheets that are referenced.
The list is a simple copy of the SKU column from your big list. In my test I also copied the the descriptions. You may find that way easier to fill the 3rd column, which holds the words "Cool" and "Cold" (or whatever else you want) against each item. The middle column isn't used and not required y the above code.
According to your description, the 'List' sheet should be Very Hidden. In the VB Editor's Project Browser, click on the sheet, bring up its properties and set the Visible property to xlVeryHidden. The sheet can be made visible only by changing this property back to xlVisible. The property setting is saved when you save the workbook.

How to compare values between rows

I need help here. I have a spreadsheet that has more than 6K datas. I need to compare the values between the "MOVE_IN_QTY" and "MOVE_OUT_QTY" by using VBA. The problem here is I need to compare the value right after the code has changed from "CV64" and "TW78" in the code column. The value I have higlighted in red and the code I have highlighted in blue and yellow. I would appreciate any help. Thanks.
Making a few assumptions here:
Move In & Move Out are always numbers.
Move numbers are compered using the =,<,> process.
Unknown further action based on result is required.
Also it helps to include what you have tried and what is not working.
Sub ReviewData()
Dim wkbk As Workbook
Dim xsheet As Worksheet
Dim codeColumn As String, moveIN As String, moveOUT As String
Dim rowCount As Double
Set wkbk = ThisWorkbook
Set xsheet = wkbk.Worksheets("Sheet1") 'change sheet name here
codeColumn = "B" ' change column letter here
moveIN = "C" 'set move in column
moveOUT = "D" 'set move out column
'this will loop through the Code column until the last set of data.
rowCount = xsheet.Range(codeColumn & xsheet.Rows.Count).End(xlUp).Row 'find last row
For x = 2 To rowCount
'checks if code transitions from one code to another
If not xsheet.Range(codeColumn & x).Value = xsheet.Range(codeColumn & x + 1).Value Then
If xsheet.Range(moveIN & x).Value = xsheet.Range(moveOUT & x + 1).Value Then
'do something if the code is the same
Else
xsheet.Range(codeColumn & x).Interior.ColorIndex = 3
MsgBox ("Row: " & x & " is different") 'comment this out not to get the message
End If
Else
End If
Next x
End Sub

Assigning a row range to a variable

I have been trying so hard. I cant figure this out. I am working with two sheets. One sheet searches for a criteria "RR", ir there is an RR, it assigns a variable a serial to be searched in another sheet. If the serial is found in the other sheet, I would like to determine the row where it is located and assign it to a variable. "DidTransfer = Sheets(PreviousTabName).Range("B" & thiscell.Row).Value" The problem when I use thiscell.Row, its giving me so many problems. I need the row number to so I can reference the same row to get information from another cell on the same row. Please help.
Sub TempModifier()
Dim NYSID, PLookUpTabRange, IsRR, DidTransfer As String
Dim thiscell As Range
'Variable for Temp
Dim TempFirstRow As Integer
Dim TempLastRow As Long
'Variables for the previous
Dim PreviousTabLastRow As Long
Dim PreviousTabFirstRow As Integer
'Initialize the temp variables
TempLastRow = Sheets("Temp").Range("D" & Rows.Count).End(xlUp).Row
PreviousTabName = "February"
PreviousTabFirstRow = 7
With Sheets(PreviousTabName)
PreviousTabLastRow = .Cells(256, "H").End(xlUp).Row 'Get the last row in the data range
End With
'Create a data-range variable
PLookUpTabRange = "H" & PreviousTabFirstRow & ":" & "H" & PreviousTabLastRow
'Begin looping structure to copy data from the temp tab to the current tab
For TempFirstRow = 2 To TempLastRow
'Assign the value of the housing unit
IsRR = Sheets("Temp").Cells(TempFirstRow, 2).Value
'Check if the value is RR
If IsRR = "RR " Then
'If the value is RR, then get the NYSID
NYSID = Worksheets("Temp").Cells(TempFirstRow, 4).Value
If Not IsError(Application.Match(NYSID, Worksheets(PreviousTabName).Range(PLookUpTabRange), 0)) Then
'NYSID is Found on Current Month Sheet, do Nothing
Else
DidTransfer = ""
Set thiscell = Sheets(PreviousTabName).Columns("D").Find(What:=NYSID, LookIn:=xlValues, lookat:=xlWhole)
DidTransfer = Sheets(PreviousTabName).Range("B" & thiscell.Row).Value
Select Case DidTransfer
Case "Transferred"
DidTransfer = "Transferred"
Case Else
DidTransfer = DidTransfer
End Select
If IsError(Application.Match(NYSID, Worksheets(PreviousTabName).Range(PLookUpTabRange), 0)) Or _
(Not IsError(Application.Match(NYSID, Worksheets(PreviousTabName).Range(PLookUpTabRange), 0)) And _
DidTransfer = "Transferred") Then
'Worksheets("Temp").Rows(TempFirstRow).Delete
MsgBox "Delete"
End If
End If
End If
'Go to the next row
Next TempFirstRow
End Sub

Rearrange data in a column

Is there any way to automatically arrange this data
Into this
Using excel/google sheets/etc. Basically I have a huge list of files (second column) that I need to map to it's respective folder (first column ID).
What I need, is to copy column A data down, but only to the blank cells immediately below, and then do it again for the new folder id, and so on.
I happen to have a macro that prompts the user which column to copy data down. See the below (Note you may need to tweak as necessary):
Sub GEN_USE_Copy_Data_Down()
Dim screenRefresh$, runAgain$
Dim lastRow&, newLastRow&
Dim c As Range
Dim LastRowCounter$
Dim columnArray() As String
screenRefresh = MsgBox("Turn OFF screen updating while macro runs?", vbYesNo)
If screenRefresh = vbYes Then
Application.ScreenUpdating = False
Else
Application.ScreenUpdating = True
End If
Dim EffectiveDateCol As Integer
LastRowCounter = InputBox("What column has the most data (this info will be used to find the last used row")
CopyAgain:
With ActiveSheet
lastRow = .UsedRange.Rows.Count
End With
' THIS WILL ASK THE USER TO SELECT THE COLUMN TO COPY DATA DOWN
MsgBox ("Now, you will choose a column, and that column's data will be pasted in the range" & vbCrLf & "below the current cell, to the next full cell")
Dim Column2Copy As String
Column2Copy = InputBox("What columns (A,B,C, etc.) would you like to copy the data of? Use SPACES, to separate columns")
columnArray() = Split(Column2Copy)
Dim startCell As Range
For i = LBound(columnArray) To UBound(columnArray)
Debug.Print i
Column2Copy = columnArray(i)
Set startCell = Cells(1, Column2Copy).End(xlDown)
Do While startCell.row < lastRow
If startCell.End(xlDown).Offset(-1, 0).row > lastRow Then
newLastRow = lastRow
Else
newLastRow = startCell.End(xlDown).Offset(-1, 0).row
End If
Set CopyFrom = startCell
Range(Cells(startCell.row, Column2Copy), Cells(newLastRow, Column2Copy)).Value = CopyFrom.Value
Set startCell = startCell.End(xlDown)
Loop
Next i
If screenRefresh = vbYes Then
Application.ScreenUpdating = True
Else
Application.ScreenUpdating = True
End If
End Sub
I wrote it a while ago, so it might be able to have lines removed/combined, but it should work (assuming you're trying to just copy data down column A).
In Excel, select the left-hand column, HOME > Editing, Find & Select, Go to Special..., check Blanks (only), OK, then select one of the chosen cells, =, Up, Ctl+Enter.

Resources