Macro to Copy Row if Highlighted and Concatenate - vba

I have an Excel sheet with data I would like to concatenate and use to create an .ini file.
There are multiple columns with data, if a cell in column D is highlighted I want it to copy the data in that row on to another sheet but at the same time I want it to concatenate the data in each column with the column header, see below:
From the picture above I would like the macro to copy the data into another sheet in the following format:
name = Machine 1
caption = Presentation
make = Company 1
model = Model 1
Is this possible?

If you want to create a ini file and not a sheet with ini look. Use this code:
LastRow = Range("A" & Rows.Count).End(xlUp).Row
for i = 3 to LastRow
If range("A" & i).Interior.ColorIndex = 2 then
MyFile = "C:\inifiles\" & Range("C" & i).Value & ".ini" 'Machine 1.ini
fnum = FreeFile()
Open MyFile For Output As #fnum
Print #fnum, "name=" & Range("C" & i).Value
Print #fnum, "caption=" & Range("D" & i).Value
Print #fnum, "make=" & Range("E" & i).Value
Print #fnum, "model=" & Range("F" & i).Value
Close #fnum
End if
Next i
It loops through all rows and uses the data to create a file with the "name" as the name of the file.
EDIT:
If you want to create the sheets with ini look:
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 3 To LastRow
If range("A" & i).Interior.ColorIndex = 2 then
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = Sheets(1).Range("C" & i).Value
Sheets(Sheets(1).Range("C" & i).Value).Range("A1") = "name=" & Sheets(1).Range("C" & i).Value
Sheets(Sheets(1).Range("C" & i).Value).Range("A2") = "caption=" & Sheets(1).Range("D" & i).Value
Sheets(Sheets(1).Range("C" & i).Value).Range("A3") = "make=" & Sheets(1).Range("E" & i).Value
Sheets(Sheets(1).Range("C" & i).Value).Range("A4") = "model=" & Sheets(1).Range("F" & i).Value
End if
Next i

Related

Rearranging columns and exIporting range to CSV

I have a set of data that is downloaded from SQL Server. Data can be huge. I need to rearrange it to a specific column order template before exporting it to either CSV or XLSX, really does not matter.
In this instance, I have put together a code that takes the original data (From Col A to Co; Q and set them up in the same sheet from Col T to Col AB) then attempt to export that range (T:AB to Last row) as CSV.
First part of the code works, pretty straightforward. However, I am struggling to export the range to either CSV or XLSX. Currently it is exporting data into another file only in row 1.
CODE
Sub test()
Dim LR As Long
Dim myCSVFileName As String
Dim myWB As Workbook
Dim rngToSave As range
Dim fNum As Integer
Dim csvVal As String
LR = Sheets("AAP").Cells(Rows.count, "A").End(xlUp).row
Sheets("AAP").range("T2:T" & LR).Value = Sheets("AAP").range("B2:B" & LR).Value
Sheets("AAP").range("U2:U" & LR).Value = Sheets("AAP").range("C2:C" & LR).Value
Sheets("AAP").range("V2:V" & LR).Value = Sheets("AAP").range("I2:I" & LR).Value
Sheets("AAP").range("W2:W" & LR).Value = Sheets("AAP").range("J2:J" & LR).Value
Sheets("AAP").range("X2:X" & LR).Value = Sheets("AAP").range("E2:E" & LR).Value
Sheets("AAP").range("Y2:Y" & LR).Value = Sheets("AAP").range("F2:F" & LR).Value
Sheets("AAP").range("Z2:Z" & LR).Value = Sheets("AAP").range("H2:H" & LR).Value
Sheets("AAP").range("AA2:AA" & LR).Value = Sheets("AAP").range("G2:G" & LR).Value
Sheets("AAP").range("AB2:AB" & LR).Value = "AA_FEES"
Set myWB = ThisWorkbook
myCSVFileName = myWB.Path & "\" & "CSV-Exported-File-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
csvVal = ""
fNum = FreeFile
Set rngToSave = range("T2:AB" & LR)
Open myCSVFileName For Output As #fNum
For i = 1 To rngToSave.Rows.count
For j = 1 To rngToSave.Columns.count
csvVal = csvVal & Chr(34) & rngToSave(i, j).Value & Chr(34) & ","
Next
Print #fNum, Left(csvVal, Len(csvVal) - 2)
csvVal = ""
Next
Close #fileNumber
End Sub
Any advise how to make this more efficient would be greatly appreciated.
The easiest method to export that to a CSV would be to copy the worksheet to no destination. This creates a new workbook that is the new ActiveWorkbook with a single worksheet that is a copy of the original. After deleting columns A:S, SaveAs xlCSV.
...
workSheets("AAP").copy
with activeworkbook
application.displayalerts = false
.worksheets(1).range("A:S").entirecolumn.delete
.saveas filename:=myCSVFileName, fileformat:=xlcsv
.close savechanges:=false
application.displayalerts = true
end with

Summing Two Columns - Type mismatch Error

I am trying to sum two columns, but i keep getting an error message of type mismatch. Where my error is?
Sub SumCols()
Dim ws As Worksheet
Set ws = Sheets("Recon")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
Range("E" & i).Value = Range("C" & i).Value + Range("D" & i).Value
Next i
End Sub
The below might be my issue, I checked for blank cells and non were found. But I can see blank cells.
Most likely one of the values you are trying to add together is not numeric, so check to see if they are numeric and non-blank before you try adding them.
For i = 2 To LastRow
If Len(Range("C" & i).Value) > 0 And Len(Range("D" & i).Value) > 0 Then
If IsNumeric(Range("C" & i).Value) And IsNumeric(Range("D" & i).Value) Then
Range("E" & i).Value = Range("C" & i).Value + Range("D" & i).Value
End If
End If
Next i
Also, you might be better off just using a formula:
Range("E" & i).Formula = "=C" & i & "+D" & i

vba to copy to different ranges

I have this sheet called Consulta where everytime I change the value on the column K it changes the color of the range E:K to green or white if it's empty.
I also want to if the row is green, copy that row to the sheet called E-mail. This is what I've tried so far and it works:
Sub ChangeColor()
Dim ws As Worksheet, ws1 As Worksheet, i As Long, lastrow As Long
Set ws = Sheets("Consulta")
Set ws1 = Sheets("E-mail")
lastrow = ws.Cells(Rows.Count, "E").End(xlUp).Row
For i = 5 To lastrow
If ws.Range("K" & i) <> "" Then
ws.Range("E" & i & ":K" & i).Interior.ColorIndex = 43
ws.Range("E" & i & ":K" & i).Copy ws1.Range("A" & i & ":G" & i)
Else
ws.Range("E" & i & ":K" & i).Interior.ColorIndex = 2
End If
Next
If ws.Range("E" & i & ":K" & i).Interior.ColorIndex = 2 Then
ws1.Range("A" & i & ":G" & i).Clear
End If
End Sub
My problem is with this line below:
ws.Range("E" & i & ":K" & i).Copy ws1.Range("A" & i & ":G" & i)
I actually want to copy to a different range instead of the corresponding range in the sheet E-mail (for example, if the first match is E3:K3 I want to copy to A2:K2. If the second match is E34:K34 I want to copy it to A3:K3 and so it goes).
I tried using another loop but my Excel got crazy so I think I did it wrong.
Any suggestions will be appreciated.
You only need the upper-left corner cell for a destination. Look from the bottom up for the last used cell and offset down a row.
with ws1
ws.Range("E" & i & ":K" & i).Copy .cells(.rows.count, "A").end(xlup).offset(1, 0)
end with
You might want to put this above the line that applies a fill color or you will be copying the fill color as well.

Insert data to table from form

I created a simple form in Excel for inserting data to the spreadsheet. I have a table in my sheet with headlines in row 2 and blank in row 3. When I enter the data form into the first empty row, it started from row 4 (fist row under the table). I want to insert data into existing table. How can I do this?
This is Add button code in my form:
Private Sub Add_Click()
TrackingDate = UserForm1.TrackingDate.Value
INS = UserForm1.INS.Value
COY = UserForm1.COY.Value
Amount = UserForm1.Amount.Value
InvoiceDate = UserForm1.InvoiceDate.Value
InvoiceNumber = UserForm1.InvoiceNumber.Value
PolicyNumber = UserForm1.PolicyNumber.Value
Reminder = UserForm1.Reminder.Value
Cheque = UserForm1.Cheque.Value
Status = UserForm1.Status.Value
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = TrackingDate
Range("B" & Rows.Count).End(xlUp).Offset(1).Value = INS
Range("C" & Rows.Count).End(xlUp).Offset(1).Value = COY
Range("D" & Rows.Count).End(xlUp).Offset(1).Value = Amount
Range("E" & Rows.Count).End(xlUp).Offset(1).Value = InvoiceDate
Range("F" & Rows.Count).End(xlUp).Offset(1).Value = InvoiceNumber
Range("G" & Rows.Count).End(xlUp).Offset(1).Value = PolicyNumber
Range("H" & Rows.Count).End(xlUp).Offset(1).Value = Reminder
Range("I" & Rows.Count).End(xlUp).Offset(1).Value = Cheque
Range("J" & Rows.Count).End(xlUp).Offset(1).Value = Status
End Sub
your code can be written like this:
Private Sub Add_Click()
Dim i&: i = [A:J].Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1 'find the last empty row range("A:J")
With UserForm1
Range("A" & i).Value = .TrackingDate.Value
Range("B" & i).Value = .INS.Value
Range("C" & i).Value = .COY.Value
Range("D" & i).Value = .Amount.Value
Range("E" & i).Value = .InvoiceDate.Value
Range("F" & i).Value = .InvoiceNumber.Value
Range("G" & i).Value = .PolicyNumber.Value
Range("H" & i).Value = .Reminder.Value
Range("I" & i).Value = .Cheque.Value
Range("J" & i).Value = .Status.Value
End With
End Sub
When you use Range("A" & Rows.Count).End(xlUp) approaching an Excel "table", the first cell that registers will be the last cell of the "table" even if that cell is blank. I haven't tested it but suspect if your "table" is all values then it won't auto-extend itself if something is entered below a blank row.
You could try adding an innocuous formula like =Row() in the last column of your table and hiding that column - then the table should auto-extend.

Copy data to new workbook and add specific text to each row´s value in a specific column

I am exporting data from one workbook to another workbook to T13:Tlastrow
This data, from column F in my workbook where I run this macro, I want to be put into {nyckel="TEXT HERE";} in column T in the "new" workbook, starting from row 13 (T13).
I am stuck here. So would really appreciate some help/solution. Thanks!
Sub CopyData()
Dim wkbCurrent As Workbook, wkbNew As Workbook
Set wkbCurrent = ActiveWorkbook
Dim valg, c, LastCell As Range
Set valg = Selection
Dim wkbPath, wkbFileName, lastrow As String
Dim LastRowInput As Long
Dim lrow, rwCount, lastrow2, LastRowInput2 As Long
Application.ScreenUpdating = False
' If nothing is selected in column A
If Selection.Columns(1).Column = 1 Then
wkbPath = ActiveWorkbook.Path & "\"
wkbFileName = Dir(wkbPath & "CIF LISTEN.xlsm")
Set wkbNew = Workbooks.Open(wkbPath & "CIF LISTEN.xlsm")
'Application.Run ("'C:\Users\niclas.madsen\Desktop\TEST\CIF LISTEN.xlsm'!DelLastRowData")
LastRowInput = Cells(Rows.count, "A").End(xlDown).Row
For Each c In valg.Cells
lrow = wkbNew.Worksheets(1).Range("B1").Offset(wkbNew.Worksheets(1).Rows.count - 1, 0).End(xlUp).Row + 1
lastrow2 = Range("A" & Rows.count).End(xlUp).Row
lastrow3 = Range("T" & Rows.count).End(xlUp).Row
wkbCurrent.ActiveSheet.Range("E" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("A" & lrow)
wkbCurrent.ActiveSheet.Range("A" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("B" & lrow)
wkbCurrent.ActiveSheet.Range("F" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("T" & lrow)
' Standard inputs
wkbNew.Worksheets(1).Range("D13:D" & lastrow2).Value = "Ange referens och period"
wkbNew.Worksheets(1).Range("E13:E" & lastrow2).Value = "99999002"
wkbNew.Worksheets(1).Range("G13:G" & lastrow2).Value = "EA"
wkbNew.Worksheets(1).Range("H13:H" & lastrow2).Value = "2"
wkbNew.Worksheets(1).Range("M13:M" & lastrow2).Value = "SEK"
wkbNew.Worksheets(1).Range("N13:N" & lastrow2).Value = "sv_SE"
wkbNew.Worksheets(1).Range("P13:P" & lastrow2).Value = "TRUE"
wkbNew.Worksheets(1).Range("Q13:Q" & lastrow2).Value = "TRUE"
wkbNew.Worksheets(1).Range("S13:S" & lastrow2).Value = "Catalog_extensions"
'wkbNew.Worksheets(1).Range("T" & lastrow3).Value = "{Nyckelord=" & wkbNew.Worksheets(1).Range("T" & lastrow3).Value & ";}"
Next
' Trying to get this to work
LastRowInput2 = wkbNew.Worksheets(1).Range("T" & wkbNew.Sheets("Sheet1").UsedRange.Rows.count + 1).End(xlUp).Row
For i = 0 To LastRowInput2 - 13
wkbNew.Worksheets(1).Range("T" & 13 + i).Value = "{Nyckelord=" & wkbNew.Worksheets(1).Range("T" & 13 + i).Value & ";}"
Next i
' END HERE
' wkbNew.Close False
' Find the number of rows that is copied over
wkbCurrent.ActiveSheet.Activate
areaCount = Selection.Areas.count
If areaCount <= 1 Then
MsgBox "The selection contains " & Selection.Rows.count & " suppliers."
' Write it in A10 in CIF LISTEN
wkbNew.Worksheets(1).Range("A10").Value = "COMMENTS: " & Selection.Rows.count & " Suppliers Added"
Else
i = 1
For Each A In Selection.Areas
'MsgBox "Area " & I & " of the selection contains " & _
a.Rows.count & " rows."
i = i + 1
rwCount = rwCount + A.Rows.count
Next A
MsgBox "The selection contains " & rwCount & " suppliers."
' Write it in A10 in CIF LISTEN
wkbNew.Worksheets(1).Range("A10").Value = "COMMENTS: " & rwCount & " Suppliers Added"
End If
wkbNew.Worksheets(1).Activate
Application.ScreenUpdating = True
Else
MsgBox "Please select cell(s) in column A", vbCritical, "Error"
Exit Sub
End If
End Sub
OK Try
wkbNew.Worksheets(1).Range("T" & lrow).Value = "{Nyckelord=" & wkbCurrent.ActiveSheet.Range("F" & c.Row).Value & "}"
Instead of your line:
wkbCurrent.ActiveSheet.Range("F" & c.Row).Copy Destination:=wkbNew.Worksheets(1).Range("T" & lrow)
And remove the whole block marked 'Trying to get this to work
If the code is doing the right action but on the wrong cells, then the problem is in the start and end of the For loop. Your For Loop is going from row '13 + i' where i = 0 (so row 13), to row 13 + LastRowInput2 - 13 (so LastRowInput2). This seems right to me, so the problem must be with the value in LastRowInput2.
You need to correct this line:
LastRowInput2 = wkbNew.Worksheets(1).Range("T" & wkbNew.Sheets("Sheet1").UsedRange.Rows.count + 1).End(xlUp).Row
So that is gives you the correct last row input in your data. There are several approaches to finding the end of data depending on whether there may be blank cells in the middle and other factors. This may be one option:
LastRowInput2 = wkbNew.Worksheets(1).Range("T65000").End(xlUp).Row
Be sure to step through the code and verify that LastRowInput2 is set to the value you expect and then this should work.