VBA Code to paste special values over a cell depending on another cells value....

LPS ESQ

New Member
Joined
Feb 18, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello there,

Sorry been a very long time since I visited this site. Apologies.

I am trying to do something relatively simple (I think) in Excel. (Excel 365 Version 2302 Build 16.0.16130.20586) 64 bit.


Basically I have a range of cells say K4:Z4. Each cell has a formula of =if(K$3<$a$2,"yes","No"), so for M4 would be M3 etc etc...

Row 8 for each column has a =NOW() as a timestamp.

What happens is that each time data is entered into rows 24 onwards, when complete and in the correct format column(whatever) row 4 changes from "no" to "yes" based upon formulae.

What I want to happen, and this is where I am struggling - it's probably so obvious that I cannot see it is upon saving a macro runs so that every row 8, column whatever cell, where row 4 is "yes" overwrites the =NOW() with copy paste special values & formats.

The code I have so far is this: (Yes I am a few years out of VBA coding so it is a tad bodged! - so much so, it doesn't work.) Any help so that it checks K4 and if "YES", copy paste special value of K8 into K8 then moves onto L4 etc etc... )

Many thanks in advance.









Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Sheet1.Select
Range("E6").Select
Selection.Copy
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False


'Option Explicit

'Sub APM()

Sheet1.Select
Dim i As Long
For i = 11 To 500
If Cells(i, 4) = "Yes" Then
Cells(i, 8).Copy
Cells(i, 8).PasteSpecial xlPasteValues
Exit For
Next i
'End If

End If
Call GetUserName_Environ
End Sub





Sub GetUserName_Environ()

Dim x As Integer

x = Sheet1.Range("A1").Value

For idx = 1 To 255
strEnvironVal = VBA.Interaction.Environ$(idx)
ThisWorkbook.Sheets("DATA").Cells(idx + 5, x) = strEnvironVal
Next idx

End Sub
 

Attachments

  • mrexcelcapture.JPG
    mrexcelcapture.JPG
    163.1 KB · Views: 9

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Maybe it's just that you appear to have your row,column references backwards? Stepping through your code and checking your variables and reference values will often reveal what the issues are.
BTW, if you post code please do so within code tags (use vba button on posting toolbar) to maintain indentation and readability. Also, you don't have to select and copy. It is often enough to just make Sheets("Sheet1).Range("B2") = Sheets("Sheet2).Range("A1") as a rough example.
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,035
Members
449,092
Latest member
ikke

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