I have the code below but once its executed I need to clear all Columns from A to G from the ith row
Sub GetPic()
Dim fNameAndPath As String
Dim img As Object
Dim CommodityName1 As String, T1 As String
Dim myDir As String
Dim i As Integer, j As Integer
Worksheets("Picture").Activate
Dim shape As Excel.shape
Dim datarangeb As Range
Dim numberofcells As Integer
Set datarangeb = Sheets("Data").Range("b:b")
numberofcells = WorksheetFunction.CountA(datarangeb)
numberofcells = numberofcells * 12 + 1
For Each shape In ActiveSheet.Shapes
shape.Delete
Next
j = 7
For i = 2 To numberofcells
myDir = "C:\Users\User\Desktop\ESTIMATING SHEETS\test\rebar shapes" & "\"
CommodityName1 = Range("a" & i)
T1 = ".png"
fNameAndPath = myDir & CommodityName1 & T1
On Error GoTo errormessage:
Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
On Error GoTo errormessage:
With img
'Move and Resize Image
.ShapeRange.LockAspectRatio = msoFalse
.Left = ActiveSheet.Range("d" & i).Left
.Top = ActiveSheet.Range("d" & i).Top
.Width = ActiveSheet.Range("d" & i & ":g" & i).Width
.Height = ActiveSheet.Range("d" & i & ":g" & j).Height
errormessage:
If Err.Number = 1004 Then
Exit Sub
MsgBox "File does not exist." & vbCrLf & "Check the name of the rebar!"
End If
End With
Application.ScreenUpdating = True
i = i + 11
j = j + 12
Next i
i = i - 1
End Sub
So Im not too sure what the syntax is to clear up to the end of column G. One way is to just clear it up to G6000 or some large number but I want the code to execute not using too much memory
Sub schedulemacro()
Dim k As Integer
Dim data As Worksheet, Picture As Worksheet, sheet1 As Worksheet
Dim i As Integer, j As Integer
Set data = Sheets("Data")
Set Picture = Sheets("Picture")
Set sheet1 = Sheets("Sheet 1")
j = 13
Picture.Activate
data.Cells(2, 2) = sheet1.Cells(2, 1)
k = WorksheetFunction.CountA(sheet1.Range("b:b")) - 1
sheet1.Range("a2:k" & k + 1).copy
data.Range("b2:k" & k + 1).PasteSpecial xlPasteValues
If k = 0 Then
MsgBox ("Please place data into the data sheet or export from revit!")
data.Activate
data.Range("a2:l2").Select
Exit Sub
End If
Picture.Range("A1:G12").Select
Selection.copy
For i = 1 To k
data.Range("a" & i + 1) = i
Picture.Range("a" & j).PasteSpecial
j = j + 12
Next i
j = 1
i = 2
k = k + 1
For i = 2 To k
Picture.Cells(j, 1) = data.Range("A" & i)
j = j + 12
Next i
Call GetPic
End Sub
Sub GetPic()
Dim fNameAndPath As String
Dim img As Object
Dim CommodityName1 As String, T1 As String
Dim myDir As String
Dim i As Integer, j As Integer
Worksheets("Picture").Activate
Dim shape As Excel.shape
Dim datarangeb As Range
Dim numberofcells As Integer
Set datarangeb = Sheets("Data").Range("b:b")
numberofcells = WorksheetFunction.CountA(datarangeb)
numberofcells = (numberofcells - 1) * 12 + 1
For Each shape In ActiveSheet.Shapes
shape.Delete
Next
j = 7
For i = 2 To numberofcells
myDir = "C:\Users\User\Desktop\ESTIMATING SHEETS\test\rebar shapes" & "\"
CommodityName1 = Range("a" & i)
T1 = ".png"
fNameAndPath = myDir & CommodityName1 & T1
On Error GoTo errormessage:
Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
On Error GoTo errormessage:
With img
'Move and Resize Image
.ShapeRange.LockAspectRatio = msoFalse
.Left = ActiveSheet.Range("d" & i).Left
.Top = ActiveSheet.Range("d" & i).Top
.Width = ActiveSheet.Range("d" & i & ":g" & i).Width
.Height = ActiveSheet.Range("d" & i & ":g" & j).Height
errormessage:
If Err.Number = 1004 Then
Exit Sub
MsgBox "File does not exist." & vbCrLf & "Check the name of the rebar!"
End If
End With
Application.ScreenUpdating = True
i = i + 11
j = j + 12
Next i
i = i - 1
Worksheets("Picture").Range("A" & i & ":i27000").Clear
End Sub
Related
I have two inclusions I want to include in this code.
The first is to omit zero balance values (in column F in TB New) rows from my TB New and paste rows to TB New Non Zero, I will just change the input worksheet from TB New to TB New Non Zero. I was thinking of just repeating this code but changing it to new variables and just do Cell Value <> 0 but I tried it and it didnt work:
Dim xTb As Range
Dim xTbCell As Range
Dim F As Long
Dim G As Long
Dim H As Long
F = Worksheets("TB New").UsedRange.Rows.Count
G = Worksheets("TB New Non Zero").UsedRange.Rows.Count
If G = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("TB New Non Zero").UsedRange) = 0 Then G = 0
End If
Set xTb = Worksheets("TB New").Range("F1:F" & F)
On Error Resume Next
Application.ScreenUpdating = False
Worksheets("TB New Non Zero").UsedRange.Offset(1).Clear
G = 0
For H = 1 To xTb.Count
If CStr(xTb(H).Value) <> 0 Then
xTb(H).EntireRow.Copy Destination:=Worksheets("TB New Non Zero").Range("F" & G + 1)
G = G + 1
End If
Next
The code above prints just all the rows and omits some rows at the bottom which is not what I want.
The second is to create a for loop where LEFT(column A Value,1) = 0 then copy the rows that are that then the next row Set value to be Expenses and then add 1 to the row then where LEFT(column A Value,1) = 1 then copy the rows then so on.
Essentially Currently it is:
0575 Interest 20
1015 Purchases -50
1680 Repairs -10
2000 Bank 200
2400 Debtors 0
2475 Plant 200
But I want it as
Revenue
0575 Interest 20
Expenses
1015 Purchases -50
1680 Repairs -10
Current Assets
2000 Bank 200
2475 Plant 200
Any help on this code? Thanks,
Function getLastUsedRow(ws As Worksheet) As Long
Dim lastUsedRow As Long: lastUsedRow = 1
On Error Resume Next
lastUsedRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
On Error GoTo 0
getLastUsedRow = lastUsedRow
End Function
Function getLastUsedColumn(ws As Worksheet) As Long
Dim lastUsedColumn As Long: lastUsedColumn = 1
On Error Resume Next
lastUsedColumn = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
On Error GoTo 0
getLastUsedColumn = lastUsedColumn
End Function
Function checkIfWorksheetExists(wb As Workbook, wsName As String) As Boolean
Dim i As Long
Dim found As Boolean
found = False
For i = 1 To wb.Worksheets.Count
If Trim(wb.Worksheets(i).Name) = Trim(wsName) Then
found = True
Exit For
End If
Next i
checkIfWorksheetExists = found
End Function
Public Function inCollection(ByVal inputCollection As Collection, ByVal inputKey As Variant) As Boolean
On Error Resume Next
inputCollection.Item inputKey
inCollection = (Err.Number = 0)
Err.Clear
End Function
Sub CopyRowBasedOnCellValue()
Dim xRg As Range
Dim xCell As Range
Dim A As Long
Dim B As Long
Dim C As Long
A = Worksheets("SQL hl_balance").UsedRange.Rows.Count
B = Worksheets("CY amounts").UsedRange.Rows.Count
If B = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("CY amounts").UsedRange) = 0 Then B = 0
End If
Set xRg = Worksheets("SQL hl_balance").Range("A1:A" & A)
On Error Resume Next
Application.ScreenUpdating = False
Worksheets("CY amounts").UsedRange.Offset(1).Clear
B = 4
For C = 1 To xRg.Count
If CStr(xRg(C).Value) = Worksheets("Client Details").Range("C19").Value Then
xRg(C).EntireRow.Copy Destination:=Worksheets("CY amounts").Range("A" & B + 1)
B = B + 1
End If
Next
If Not checkIfWorksheetExists(ThisWorkbook, "Index") Then
MsgBox "Please create Index sheet, and run macro again!"
Exit Sub
End If
Application.ScreenUpdating = False
Dim i As Long
Dim j As Long
Dim k As Long
Dim wsInputData As Worksheet: Set wsInputData = Worksheets("TB New")
Dim wsInputDataStartingRow As Long: wsInputDataStartingRow = wsInputData.Range("C1").End(xlDown).Row + 1 ' Starting row !
Dim wsInputDataEndingRow As Long: wsInputDataEndingRow = getLastUsedRow(wsInputData) + 10 ' Ending row !
Dim wsInputDataUsedRange As Range: Set wsInputDataUsedRange = wsInputData.Range("A" & CStr(wsInputDataStartingRow) & ":" & "D" & CStr(wsInputDataEndingRow))
Dim wsIndex As Worksheet: Set wsIndex = ThisWorkbook.Worksheets("Index")
Dim wsIndexStartingRow As Long: wsIndexStartingRow = 5
Dim wsIndexEndingRow As Long: wsIndexEndingRow = getLastUsedRow(wsIndex) + 10
Dim wsIndexUsedRange As Range: Set wsIndexUsedRange = wsIndex.Range("A" & CStr(wsIndexStartingRow) & ":" & "H" & CStr(wsIndexEndingRow))
Dim wsIndexSheetCollection As Collection: Set wsIndexSheetCollection = New Collection
Dim wsIndexCellCollection As Collection: Set wsIndexCellCollection = New Collection
Dim wsIndexCellCollection2 As Collection: Set wsIndexCellCollection2 = New Collection
Dim wsIndexStatusCollection As Collection: Set wsIndexStatusCollection = New Collection
For i = wsIndexStartingRow To wsIndexEndingRow
If Trim(wsIndex.Range("A" & CStr(i)).Value2) <> "" And Trim(wsIndex.Range("E" & CStr(i)).Value2) <> "" Then
If wsIndex.Range("E" & CStr(i)).Hyperlinks.Count > 0 Then
If Not inCollection(wsIndexSheetCollection, Trim(wsIndex.Range("A" & CStr(i)).Value2)) Then
wsIndexSheetCollection.Add Trim(wsIndex.Range("E" & CStr(i)).Hyperlinks.Item(1).SubAddress), Trim(wsIndex.Range("A" & CStr(i)).Value2)
wsIndexCellCollection2.Add Trim(wsIndex.Range("E" & CStr(i)).Value2), Trim(wsIndex.Range("A" & CStr(i)).Value2)
End If
Else
If Not inCollection(wsIndexSheetCollection, Trim(wsIndex.Range("A" & CStr(i)).Value2)) Then
wsIndexSheetCollection.Add Trim(wsIndex.Range("E" & CStr(i)).Value2), Trim(wsIndex.Range("A" & CStr(i)).Value2)
wsIndexCellCollection2.Add Trim(wsIndex.Range("E" & CStr(i)).Value2), Trim(wsIndex.Range("A" & CStr(i)).Value2)
End If
End If
End If
If Trim(wsIndex.Range("A" & CStr(i)).Value2) <> "" And Trim(wsIndex.Range("F" & CStr(i)).Value2) <> "" Then
If Not inCollection(wsIndexCellCollection, Trim(wsIndex.Range("A" & CStr(i)).Value2)) Then
wsIndexCellCollection.Add Trim(wsIndex.Range("F" & CStr(i)).Value2), Trim(wsIndex.Range("A" & CStr(i)).Value2)
End If
End If
If Trim(wsIndex.Range("A" & CStr(i)).Value2) <> "" And Trim(wsIndex.Range("H" & CStr(i)).Value2) <> "" Then
If Not inCollection(wsIndexStatusCollection, Trim(wsIndex.Range("A" & CStr(i)).Value2)) Then
wsIndexStatusCollection.Add Trim(wsIndex.Range("H" & CStr(i)).Value2), Trim(wsIndex.Range("A" & CStr(i)).Value2)
End If
End If
Next i
Call Unlock_Sheet
wsIndexUsedRange.ClearContents
wsIndexUsedRange.Cells.Font.Bold = False
wsInputDataUsedRange.Copy
wsIndex.Range("A" & CStr(wsIndexStartingRow)).PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
wsIndexEndingRow = getLastUsedRow(wsIndex) + 100
For i = wsIndexEndingRow To wsIndexStartingRow Step -1
If Trim(wsIndex.Range("B" & CStr(i)).Value2) = "" Then
wsIndex.Rows(CStr(i) & ":" & CStr(i)).Delete
End If
Next i
wsIndexEndingRow = getLastUsedRow(wsIndex) + 10
For i = wsIndexStartingRow To wsIndexEndingRow
If Trim(wsIndex.Range("A" & CStr(i)).Value2) <> "" Then
If inCollection(wsIndexSheetCollection, Trim(wsIndex.Range("A" & CStr(i)).Value2)) Then
If InStr(1, wsIndexSheetCollection.Item(Trim(wsIndex.Range("A" & CStr(i)).Value2)), "!") <> 0 Then
wsIndex.Hyperlinks.Add Anchor:=wsIndex.Range("E" & CStr(i)), Address:="", SubAddress:=wsIndexSheetCollection.Item(Trim(wsIndex.Range("A" & CStr(i)).Value2)), TextToDisplay:="'" & Trim(wsIndex.Range("A" & CStr(i)).Value2)
wsIndex.Range("E" & CStr(i)).Value2 = "'" & wsIndexCellCollection2.Item(Trim(wsIndex.Range("A" & CStr(i)).Value2))
Else
If checkIfWorksheetExists(ThisWorkbook, Trim(wsIndex.Range("A" & CStr(i)).Value2)) Then
If inCollection(wsIndexCellCollection, Trim(wsIndex.Range("A" & CStr(i)).Value2)) Then
wsIndex.Hyperlinks.Add Anchor:=wsIndex.Range("E" & CStr(i)), Address:="", SubAddress:=Trim(wsIndex.Range("A" & CStr(i)).Value2) & "!" & wsIndexCellCollection.Item(Trim(wsIndex.Range("A" & CStr(i)).Value2)), TextToDisplay:=Trim("'" & wsIndex.Range("A" & CStr(i)).Value2)
wsIndex.Range("E" & CStr(i)).Value2 = "'" & wsIndexCellCollection2.Item(Trim(wsIndex.Range("A" & CStr(i)).Value2))
Else
wsIndex.Hyperlinks.Add Anchor:=wsIndex.Range("E" & CStr(i)), Address:="", SubAddress:=Trim(wsIndex.Range("A" & CStr(i)).Value2) & "!A1", TextToDisplay:="'" & Trim(wsIndex.Range("A" & CStr(i)).Value2)
wsIndex.Range("E" & CStr(i)).Value2 = "'" & wsIndexCellCollection2.Item(Trim(wsIndex.Range("A" & CStr(i)).Value2))
End If
Else
wsIndex.Range("E" & CStr(i)).Value2 = wsIndexSheetCollection.Item(Trim(wsIndex.Range("A" & CStr(i)).Value2))
End If
End If
End If
If inCollection(wsIndexCellCollection, Trim(wsIndex.Range("A" & CStr(i)).Value2)) Then
wsIndex.Range("F" & CStr(i)).Value2 = wsIndexCellCollection.Item(Trim(wsIndex.Range("A" & CStr(i)).Value2))
End If
wsIndex.Range("G" & CStr(i)).Formula = "=IFERROR(IF(OR(C" & CStr(i) & "+D" & CStr(i) & "=INDIRECT(""'""&E" & CStr(i) & "&""'""&""!""& F" & CStr(i) & "),D" & CStr(i) & "-C" & CStr(i) & "=INDIRECT(""'""&E" & CStr(i) & "&""'""&""!""& F" & CStr(i) & "),C" & CStr(i) & "-D" & CStr(i) & "=INDIRECT(""'""&E" & CStr(i) & "&""'""&""!""& F" & CStr(i) & ")),1,0),"""")"
If inCollection(wsIndexStatusCollection, Trim(wsIndex.Range("A" & CStr(i)).Value2)) Then
wsIndex.Range("H" & CStr(i)).Value2 = wsIndexStatusCollection.Item(Trim(wsIndex.Range("A" & CStr(i)).Value2))
End If
End If
Next i
Set wsIndexSheetCollection = Nothing
Set wsIndexCellCollection = Nothing
Set wsIndexStatusCollection = Nothing
Set wsIndex = Nothing
Sheets("Index").Select
Range("A1").Select
MsgBox "Done !", vbInformation
Application.ScreenUpdating = True
End Sub```
[1]: https://i.stack.imgur.com/H1jT6.png
I would like to create Line charts (2-D Line with Markers) for a set of records.
Screenshot of Excel tab
(a) Every three rows represent a set to be charted. The fourth column, though it appears, need not be charted. In the screenshot there are 18 rows, which is six sets of records.
(b) One Line chart required for each set of records. Hence a total of six charts to be created automatically.
(c) Also, the charts are to be created in different Excel tabs. Three charts per tab, hence this should create two Excel tabs with three charts placed in each tab.
I have prepared some code for automatic charts generation using VBA macro in excel see if this helps.
Below code will generate charts for data available in excel:
Dim Startrow As String
Dim Lastrow As String
Dim Lastcolumn As String
Dim ws1 As String
Dim cs2 As String
Dim cs3 As String
Dim ws(100)
Dim count As String
Dim i As Integer
Sub Final()
For j = 2 To Sheets.count
count = Sheets.count
Sheet1.Activate
Cells(i, 1) = Sheets(i).Name
'ws(i) = Sheets(i).Name
'MsgBox Sheets.Count
'MsgBox count
'MsgBox ws(i)
Next
For i = 2 To count
MsgBox count
MsgBox i
Sheet1.Activate
ws(i) = Cells(i, 1)
Sheets(ws(i)).Activate
'Sheets(ws(i)).Activate
MsgBox ws(i)
'MsgBox Range("B1")
'MsgBox IsEmpty(Range("B2"))
If IsEmpty(Range("B1")) = False Then
Startrow = 2
'MsgBox Startrow
Lastrow = Cells(Rows.count, 1).End(xlUp).Row
'MsgBox Lastrow
Lastcolumn = Split(Columns(Range("A1").End(xlToRight).Column).Address(, False), ":")(1)
'MsgBox Lastcolumn
Call test1
MsgBox i
End If
Next
End Sub
Function test1()
Dim letter As String
Dim letter1 As String
Dim letter2 As String
Dim letter3 As String
Dim x As Integer
MsgBox ws(i)
x = Range(Lastcolumn & 1).Column
'MsgBox x
Dim cs As Worksheet
Set cs = ThisWorkbook.Sheets.Add
'ws.name = "PrivateBytes_000005_Charts"
'ws2 = "PrivateBytes_000005_Charts"
cs.Name = ws(i) + "_Charts"
'MsgBox ws.Name
cs2 = ws(i) + "_Charts"
MsgBox cs2
cs3 = ws(i)
MsgBox cs3
If x < 27 Then
For i = 2 To x
letter3 = Chr(64 + i)
'chart command
Sheets(cs2).Activate
ActiveSheet.Shapes.AddChart2(227, xlLineStacked).Select
ActiveChart.SetSourceData Source:=Sheets(cs3).Range("A" & Startrow & ":A" & Lastrow & "," & letter3 & Startrow & ":" & letter3 & Lastrow)
With ActiveChart
.HasTitle = True
Sheets(cs3).Activate
.ChartTitle.Text = Range(letter3 & "1").Value
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
Next
End If
If x >= 27 Then
For i = 2 To 26
letter3 = Chr(64 + i)
'chart command
Sheets(cs2).Activate
ActiveSheet.Shapes.AddChart2(227, xlLineStacked).Select
ActiveChart.SetSourceData Source:=Sheets(cs3).Range("A" & Startrow & ":A" & Lastrow & "," & letter3 & Startrow & ":" & letter3 & Lastrow)
With ActiveChart
.HasTitle = True
Sheets(cs3).Activate
.ChartTitle.Text = Range(letter3 & "1").Value
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
Next
For i = 27 To x
letter3 = Chr(Int((i - 1) / 26) + 64) & Chr(((i - 1) Mod 26) + 65)
'MsgBox letter3
'chart command
Sheets(cs2).Activate
ActiveSheet.Shapes.AddChart2(227, xlLineStacked).Select
ActiveChart.SetSourceData Source:=Sheets(cs3).Range("A" & Startrow & ":A" & Lastrow & "," & letter3 & Startrow & ":" & letter3 & Lastrow)
With ActiveChart
.HasTitle = True
Sheets(cs3).Activate
.ChartTitle.Text = Range(letter3 & "1").Value
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
Next
End If
Sheets(cs.Name).Activate
ActiveChart.Parent.Name = "Test1"
Call AutoSpace_Shapes_Vertical
'Return
'Unload UserForm1
End Function
Function AutoSpace_Shapes_Vertical()
'Automatically space and align shapes
Sheets(cs2).Activate
ActiveSheet.ChartObjects("Test1").Activate
ActiveSheet.Shapes.SelectAll
Dim shp As Shape
Dim lCnt As Long
Dim dTop As Double
Dim dLeft As Double
Dim dHeight As Double
Const dSPACE As Double = 8
'Check if shapes are selected
If TypeName(Selection) = "Range" Then
MsgBox "Please select shapes before running the macro."
Exit Function
End If
'Set variables
lCnt = 1
'Loop through selected shapes (charts, slicers, timelines, etc.)
For Each shp In Selection.ShapeRange
With shp
'If not first shape then move it below previous shape and align left.
If lCnt > 1 Then
.Top = dTop + dHeight + dSPACE
.Left = dLeft
End If
'Store properties of shape for use in moving next shape in the collection.
dTop = .Top
dLeft = .Left
dHeight = .Height
End With
'Add to shape counter
lCnt = lCnt + 1
Next shp
End Function
'End Sub
'End Sub
enter image description here
I am building on some code, partly cut and paste from other posts. I need to concatenate with a VBA code keeping the format and running through rows to output in last cell in each row. (Can't paste image) so hope description is clear:
In A1:D1 values are RED,BLUE,GREEN
In A2:D2 Values are YELLOW,PURPLE,ORANGE
OUTPUT IN E1 should concatenate these values, keeping font colour. Each value should have "ALT ENTR" to give line break.
Next row should be displayed in E2, and so on
'************************************************************************************
Sub test()
Dim rng As Range: Set rng = Application.Range("a1:c1") 'Not yet looping
Dim row As Range
For Each row In rng.Rows
'Debug.Print col.Column
Call concatenate_cells_formats(Cells(1, 4), rng) 'Not yet looping
Next row
End Sub
Sub concatenate_cells_formats(cell As Range, source As Range)
'Anon
Dim c As Range
Dim i As Integer
i = 1
With cell
.Value = vbNullString
.ClearFormats
For Each c In source
.Value = .Value & " " & Trim(c)
Next c
.Value = Trim(.Value)
For Each c In source
With .Characters(Start:=i, Length:=Len(Trim(c))).Font
.Name = c.Font.Name
.FontStyle = c.Font.FontStyle
.Size = c.Font.Size
.Strikethrough = c.Font.Strikethrough
.Superscript = c.Font.Superscript
.Subscript = c.Font.Subscript
.OutlineFont = c.Font.OutlineFont
.Shadow = c.Font.Shadow
.Underline = c.Font.Underline
.ColorIndex = c.Font.ColorIndex
End With
.Characters(Start:=i + Len(c), Length:=1).Font.Size = 1
i = i + Len(Trim(c)) + 1
Next c
End With
End Sub
'*****************************************************************************
Option Explicit
Sub concColour()
Dim i As Long, j As Long, s As Long, l As Long, clr As Long, vals As Variant
With Worksheets("sheet4")
For i = 1 To .Cells(.Rows.Count, "A").End(xlUp).Row
vals = Application.Transpose(Application.Transpose(Intersect(.Range("A:D"), .Rows(i)).Value2))
.Cells(i, "E") = Join(vals, vbLf)
s = 1
For j = LBound(vals) To UBound(vals)
l = Len(vals(j))
clr = .Cells(i, "A").Offset(0, j - 1).Font.Color
With .Cells(i, "E").Characters(Start:=s, Length:=l).Font
.Color = clr
End With
s = s + l + 1
Next j
.Cells(i, "E").Font.Size = 4
Next i
End With
End Sub
enter image description here
I think you require something like this. Change source font and formats as per your requirement.
Sub Adding_T()
Dim lena As Integer
Dim lenc As Integer
Dim lend As Integer
Dim lene As Integer
Dim LastRow As Long
Dim nrow As Long
With Worksheets("Sheet2") 'Change sheet as per your requirement
LastRow = .Cells(.Rows.Count, "A").End(xlUp).row
For nrow = 1 To LastRow
.Range("E" & nrow) = .Range("A" & nrow).Value2 & Chr(13) & Chr(10) & .Range("B" & nrow).Value2 & _
Chr(13) & Chr(10) & .Range("C" & nrow).Value2 & Chr(13) & Chr(10) & .Range("D" & nrow).Value2
lena = Len(.Range("A" & nrow).Value2)
lenc = lena + 2 + Len(.Range("B" & nrow).Value2)
lend = lenc + 2 + Len(.Range("C" & nrow).Value2)
lene = lend + 2 + Len(.Range("D" & nrow).Value2)
For i = 1 To lena
.Range("E" & nrow).Characters(Start:=i, Length:=1).Font.Color = _
.Range("A" & nrow).Characters(Start:=i, Length:=1).Font.Color
Next i
For i = lena + 2 To lenc
.Range("E" & nrow).Characters(Start:=i, Length:=1).Font.Color = _
.Range("B" & nrow).Characters(Start:=i, Length:=1).Font.Color
Next i
For i = lenc + 2 To lend
.Range("E" & nrow).Characters(Start:=i, Length:=1).Font.Color = _
.Range("C" & nrow).Characters(Start:=i, Length:=1).Font.Color
Next i
For i = lend + 2 To lene
.Range("E" & nrow).Characters(Start:=i, Length:=1).Font.Color = _
.Range("D" & nrow).Characters(Start:=i, Length:=1).Font.Color
Next i
Next
End With
End Sub
Snapshot of trial:
EDIT: OP Preferred code does not permit looping through the Range. Amended his Sub Test() to allow looping through the range.
Sub Test2()
Dim ws As Worksheet
Dim LastRow As Long
Set ws = ThisWorkbook.ActiveSheet
Dim rng As Range
Dim row As Range
Dim rw As Long
LastRow = ws.Cells(Rows.Count, "A").End(xlUp).row
rw = 1
For rw = 1 To LastRow
Set rng = ws.Range("A" & rw & ":C" & rw)
Call concatenate_cells_formats(Cells(rw, 4), rng)
Next
End Sub
Results are as per snapshot appended here.
I got this code but it doesn't seem to run all the way to the end. Gets stuck and debugger just highlights either the Loop keyword or i = i + 1 row. What am I doing wrong?
I tried If statement or For … Next but nothing seems to work.
Sub Macro1()
'
' Macro1 Macro
'
Dim i As Integer
i = 2
Do Until i > 586
Range("B2").Formula = "=sheet2!CS" & i & ""
Range("B3").Formula = "=sheet2!CR" & i & ""
Range("B4").Formula = "=sheet2!CQ" & i & ""
Range("B5").Formula = "=sheet2!CP" & i & ""
Range("B6").Formula = "=sheet2!CO" & i & ""
Range("B7").Formula = "=sheet2!CN" & i & ""
Range("B8").Formula = "=sheet2!CM" & i & ""
Range("B9").Formula = "=sheet2!CL" & i & ""
Range("B10").Formula = "=sheet2!CK" & i & ""
Range("B11").Formula = "=sheet2!CJ" & i & ""
Range("B12").Formula = "=sheet2!CI" & i & ""
Range("B13").Formula = "=sheet2!CH" & i & ""
Range("B14").Formula = "=sheet2!CG" & i & ""
'Copy and PasteSpecial a Range
Range("AL18").Copy
Worksheets("Sheet2").Range("CV" & i & "").PasteSpecial Paste:=xlPasteValues
i = i + 1
Loop
End Sub
Doesn't seem like there's any problems with the code when I tested it..
Here's your code albeit made shorter and see if it works.
Sub Macro1()
Dim i As Long, j As Long
Dim colltr As String
For i = 2 To 586
For j = 2 To 14
colltr = Split(Cells(1, 99 - j).Address, "$")(1)
Range("B" & j).Formula = "=sheet2!" & colltr & i
Next j
'Copy and PasteSpecial a Range
Worksheets("Sheet2").Range("CV" & i & "").value = Range("AL18").value
Next i
End Sub
A Simple Slow Version
Sub LoopTrouble()
Dim i As Integer
Dim j As Integer
For i = 2 To 586
For j = 1 To 13
Sheet1.Cells(j + 1, 2) = Sheet2.Cells(i, 98 - j)
' Sheet1.Cells(j + 1, "B") = Sheet2.Cells(i, 98 - j)
' Sheet1.Range("B" & j + 1) = Sheet2.Cells(i, 98 - j)
Next
Sheet2.Cells(i, 100) = Sheet1.Cells(18, 38)
Next
End Sub
A Faster 'Semi' Array Version
Sub LoopTroubleFaster()
Dim i As Integer
Dim j As Integer
Dim vntLT As Variant
Dim vntPaste As Variant
vntLT = Sheet2.Range(Cells(2, 85), Cells(586, 97)).Value2
ReDim vntPaste(1 To 13, 1 To 1)
For i = 1 To 585
For j = 1 To 13
vntPaste(j, 1) = vntLT(i, j)
Next
Sheet1.Range("B2:B14") = vntPaste
Sheet2.Cells(i + 1, 100) = Sheet1.Cells(18, 38)
Next
End Sub
I have had to open a new question following a previous question i had to decrease and increment a number which is on the link bellow
Changing VBA macro code to change number
this is the code that i am trying to work with and i got it almost to work but somewhere its gone wrong.
Bulkwks.[B5] is M20
historywks.[a2] is the time
historywks.[b2] is the name
historywks.[C2] is m201001
Sub bulkON_Click()
Dim trnwkbk As Workbook
Dim Bulkwks As Worksheet
Dim Deswkbk As Workbook
Dim LogNum As Range, LastNum, NewNum,
Dim historywks As Worksheet
Dim nextRow As Long
Dim lOR As Long
Dim myIn As String
Dim myLeft As String
Dim myMid As Integer, myRight As Integer, i As Integer
Dim myOut As String
Set trnwkbk = Workbooks("Transport.xls")
Set Bulkwks = trnwkbk.Worksheets("Bulk")
lOR = MsgBox("Have you selected the right MIS or HUB or PSA number?", vbQuestion + vbYesNo, "Number Order")
If lOR = vbNo Then
MsgBox "Please select right Order Number"
Else
Application.ScreenUpdating = False
' for testing i just made it post in test sheet in same workbook
'Set Deswkbk = Workbooks.Open("\\dunton01\Inspections\TRANSPORT\New_transport\data\Febuary_2013.xls")
'Set historywks = Deswkbk.Worksheets("Data")
Set historywks = Worksheets("test")
Set LogNum = historywks.[C2]
With historywks
nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With
If LogNum(2, 1) = "" Then
LastNum = LogNum
Else
LastNum = LogNum(LogNum.End(xlDown).Row - 1, 1)
End If
NewNum = Bulkwks.[B5] & Val(Mid(LastNum, 2)) + 1
If LogNum(2, 1) = "" Then
LogNum(2, 1) = NewNum
Else
myIn = LogNum
myLeft = Left(myIn, 1)
myMid = CInt(Mid(myIn, 2, 2))
myRight = CInt(Right(myIn, 4))
myOut = myLeft & Format(myMid, "00") & Format(myRight, "0000")
i = 0
Debug.Print "IN: " & myIn
Debug.Print "BROKEN UP: " & myOut
Do Until myMid = -1
Debug.Print "ITERATION " & Format(i, "00") & ": " & myLeft & Format(myMid, "00") & Format(myRight, "0000")
myMid = myMid - 1
myRight = myRight + 1
myOut = myLeft & Format(myMid, "00") & Format(myRight, "0000")
i = i + 1
With historywks
'enter date and time stamp in record
With .Cells(nextRow, "A")
.Value = Now
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
'enter user name in column B
.Cells(nextRow, "B").Value = Application.UserName
.Cells(nextRow, "C").Value = myIn
End With ' for testing i just disabled this Deswkbk.save
Loop
' for testing i just disabled this
'Deswkbk.Close savechanges:=True
Application.ScreenUpdating = True
Bulkwks.[E3] = NewNum
End If
' for testing i just disabled this
'Call File_In_Network_Folder
End If
End Sub
You'll need to use the myOut variable.
.Cells(nextRow, "C").Value = myOut