Improve VBA transferring cell formatting & values

Lil Stinker

Board Regular
Joined
Feb 16, 2022
Messages
151
Office Version
  1. 2019
Platform
  1. Windows
I'm trying to build an inventory management application, so the setup is a bit complicated. My question has two parts.

I have three worksheets:
- Sheet1 "Receipt" is a form that contains a list of barcodes numbers for returned items being checked in by the user
- Sheet2 "Record" is the log where the barcode data for every return receipt generated is stored on the back end
- Sheet3 "Inventory" is where every barcode in inventory is stored and updated in or out based on the return receipt

Current process:
On the Receipt form, when a user checks in an existing barcode, the cell color with the matching barcode is changed to vbYellow, the font is changed to bold, and a diagonal line is added to "cross out" that number. If the item coming back is damaged, the cell color is changed to vbRed. For any barcode that is not returned, the font remains light grey. Previous return cells are shaded grey with white font. The visual differentiation is relevant to the user and the customer.
Book1.xlsm
ABCDEFGHIJKLMNOPQRST
12/3/2023Date Out2/13/2023Date In2/6/2013Previous ReturnLocationOC Carnival Event
2SERIAL NUMBERSApplesOrangesPears
30114016426444764716494551065318553757275943640765486721MB0069569MB211A322
40164017427544804717494851075320554057315945640865536722MB0079570MB212A348
50174018427944824718494951095328554257325947640965546724MB0119571MB214A349
60234019428544834720495451115131554357345948641065566725MB0279572MB215A350
70274022429244924723495551155333554557385950641565586728MB0289573MB216A351
80294024429444954728495651175334554657415953641665616729MB0299574MB217A352
90304029429544964731495751215343554957425954641765636730MB0309575MB219A375
6840134259446747124938510453135532572059396115654067066858MB184MB002A285MB387
6940154263446947144941510553175533572459406116654467096860MB185MB003A287MB388
70SERIAL NUMBERSApplesOrangesPears
71New ReturnPrev ReturnDamaged Return####Not Returned
Receipt
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G71Cell ValueduplicatestextNO
D71Cell ValueduplicatestextNO
C3:S4Cell ValueduplicatestextNO


The user then clicks the record button on the Receipt form and this color-coded data is transferred to the Record sheet (albeit, very slowly thanks to the copy & pastespecial code I'm currently using--did I mention I'm new at VBA?). With the Record sheet, I use an Advanced Filter in VBA to match data based on the receipt number for reloading any stored receipts and their color-coded data (also, pretty slow).
Book1.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABB
1BarcodesCriteria:uIDRA No.RTN No.Filter
2uIDPfxRA No.RTN No.sr1sr2sr3sr4sr5sr6sr7sr8sr9sr10sr11sr12sr13sr14sxb1sxb2pws1pws2RECrowRow11000002uIDPfxRA No.RTN No.sr1sr2sr3sr4sr5sr6sr7sr8sr9sr10sr11sr12sr13sr14sxb1sxb2pws1pws2RArowRow
31RR10000020114016426444764716494551065318553757275943640765486721MB0069569MB211A322331RR10000020114016426444764716494551065318553757275943640765486721MB0069569MB211A32233
41RR10000020164017427544804717494851075320554057315945640865536722MB0079570MB212A348441RR10000020164017427544804717494851075320554057315945640865536722MB0079570MB212A34844
51RR10000020174018427944824718494951095328554257325947640965546724MB0119571MB214A349551RR10000020174018427944824718494951095328554257325947640965546724MB0119571MB214A34955
61RR10000020234019428544834720495451115131554357345948641065566725MB0279572MB215A350661RR10000020234019428544834720495451115131554357345948641065566725MB0279572MB215A35066
71RR10000020274022429244924723495551155333554557385950641565586728MB0289573MB216A351771RR10000020274022429244924723495551155333554557385950641565586728MB0289573MB216A35177
81RR10000020294024429444954728495651175334554657415953641665616729MB0299574MB217A352881RR10000020294024429444954728495651175334554657415953641665616729MB0299574MB217A35288
91RR10000020304029429544964731495751215343554957425954641765636730MB0309575MB219A375991RR10000020304029429544964731495751215343554957425954641765636730MB0309575MB219A37599
101RR100000240154263446947144941510553175533572459406116654467096860MB185MB003A287MB38869101RR100000240154263446947144941510553175533572459406116654467096860MB185MB003A287MB3886910
Record
Cell Formulas
RangeFormula
BB3:BB10,X3:X10X3=ROW()


Question 1
What methods or coding can be used to improve the data transfer speed? Even as a newbie, I can tell Excel is struggling to process all the formatting. With a range potential of 1200 numbers on the receipt form, I imagine crashes are bound to happen.

Question 2
Depending on the method/code provided, how can I also have every barcode that is checked in on the Receipt form, marked "In" and for every damaged item, marked "DMG" on the Inventory sheet?

I just recently read that working with color & formatting is not advisable with data transfer and I see why however, the visual representation is crucial here. I'm open to any workarounds as long as the color formatting becomes available for the final printed Receipt form.

Inventory sample:
Book1.xlsm
ABCDEFGH
1Item IDSerialNumberItemDescriptionStatusLocationDateOutReturnDate
21011ApplesRed DeliciousOUTOC Carnival Event2/3/2023
32016ApplesGranny SmithOUTOC Carnival Event2/3/2023
43017ApplesGranny SmithOUTOC Carnival Event2/3/2023
54023ApplesRed DeliciousOUTOC Carnival Event2/3/2023
65MB006OrangesNavalINWarehouse2/6/2023
76029ApplesGranny SmithOUTOC Carnival Event2/3/2023
87A322PearsBoschOUTOC Carnival Event2/3/2023
98MB211PearsBoschINWarehouse2/6/2023
1099569OrangesNavalOUTOC Carnival Event2/3/2023
Inventory
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
It was actually another one of your posts that caught my attention (Looping through sheets, Searching for duplicate in column using ".Find"). It's why I'm looking for suggestions to work around the color transferring. I wonder though, is it just color specifically that Excel has trouble with or will any custom formatting, i.e., cell borders or font attributes, cause the same type of issues?
 
Upvote 0
The key to the problem is that VBA is very fast when things are executed in memory but it is really slow in transferring anything from the worksheet to memory or writing anything from memory back to the worksheet. However there two things which you can transfer to and from the worksheet in bulk quickly, the first is values and the second is formula. So you can load a whole huge range of values from the worksheet into memory and it takes only very slightly longer that it takes to load a single cell into memory. This is why if you want to design a fast system when using VBA you must store data as values

All formatting has to be written or tested cell by cell which is always gong to be slow.
Try running these two macros which do exactly the same thing, which is add 1 to every cells in the range A1 to A1000 in a loop of 10. Accessing the cells takes about 6.7 seconds on my computer, while the variant array solution takes 15 milliseconds on my computer.
VBA Code:
Sub speedyarray()
Dim StartTime As Double
'initialise
Worksheets("Sheet3").Select
With Worksheets("Sheet3") ' this access the worksheet
 lastrow = 1000 '
 Range(.Cells(1, 1), .Cells(lastrow, 1)) = 0 ' this access the worksheet
End With
StartTime = Timer
With Worksheets("Sheet3") ' this access the worksheet
inarr = Range(.Cells(1, 1), .Cells(lastrow, 1))  ' this access the worksheet
For j = 1 To 10
For i = 1 To lastrow  ' the loop starts here and nothing in the loop access the worksheet
 inarr(i, 1) = inarr(i, 1) + 1
Next i  ' loop ends here
Range(.Cells(1, 1), .Cells(lastrow, 1)) = inarr
Next j
'Range(.Cells(1, 4), .Cells(lastrow2, 6)) = outarr ' this access the worksheet.
End With
SecondsElapsed = (1000 * (Timer - StartTime))
MsgBox SecondsElapsed
 
End Sub
Sub speedycells()
Dim StartTime As Double
'initialise
Worksheets("Sheet3").Select
With Worksheets("Sheet3") ' this access the worksheet
 lastrow = 1000 '
 Range(.Cells(1, 1), .Cells(lastrow, 1)) = 0 ' this access the worksheet
End With
StartTime = Timer
For j = 1 To 10
For i = 1 To lastrow  ' the loop starts here and the next line in the loop accesses the worksheet
 Cells(i, 1) = Cells(i, 1) + 1
Next i  ' loop ends here
Next j
SecondsElapsed = (1000 * (Timer - StartTime))

MsgBox SecondsElapsed
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top