2014-10-08 2 views
0

"유효성 검사"로 열 이름이 있지만 열 번호가 계속 변경됩니다. 이름으로이 열을 어떻게 찾을 수 있으며 그것을 범위로 사용할 수 있습니까?범위로 열 이름을 선택하여 Excel 수식을 추가 할 VBA

현재 아래 열은 M 열을 검사하고 M 열이 비어 있지 않으면 모든 셀의 수식을 추가하는 데 사용하는 매크로입니다. 가진 자 에 대한 나의 새로운 기대가

,

  1. 참조 열 M, 다음 열 이름 "확인"을 찾아 엑셀 수식을 추가 "BLM"& "CFG"으로 셀 값이있는 경우 그 셀 값을 "BLM"& "CFG"로 지정하고 비워두면 건너 뜁니다. 셀에
  2. 변경이 모든 공식은

Sub test_macro() 
    Dim sFormula As String 
    Dim rng As Range 
    Dim ws2 As Worksheet 

    sFormula = "=IF(IFERROR(VLOOKUP(RC[-11],'Service ID Master List'!C[-11],1,0),""Fail"")=""Fail"",""Check SESE_ID"","""")&IF(IFERROR(VLOOKUP(RC[-9],Rules!C[-13],1,0),""Fail"")=""Fail"","" | Check SESE_RULE"","""")&IF(TRIM(RC[-5])="""","""",IF(IFERROR(VLOOKUP(RC[-5],Rules!C[-13],1,0),""Fail"")=""Fail"","" | Check SESE_RULE_ALT"",""""))&IF(RC[-7]=""TBD"","" | Check SEPY_ACCT_CAT"","""")" 

    Set ws2 = ActiveSheet 

    With ws2 
     Set rng = .Range("M2") 
     Set rng = .Range(rng, .Cells(.Rows.Count, rng.Column).End(xlUp)) 
    End With 
    rng.SpecialCells(xlCellTypeConstants).Offset(0, 1).FormulaR1C1 = sFormula 
    'changing formulas in values 
    Columns("N:N").Select 
    Selection.Copy 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 
    Range("N1").Select 
    Application.CutCopyMode = False 
End Sub 

답변

0

당신은 당신의 범위를 찾는 방법 찾기 을 사용할 수 있습니다 값.
뭔가 같은 :

EDIT1 :

'~~> get the position of 'Validation' column and set rng variable 
With ws2 
    Dim valCol As Long 
    valCol = .Rows("1:1").Find("Validation").Column '~~> change to suit 
    Set rng = .Range("M2") 
    Set rng = .Range(rng, .Cells(.Rows.Count, rng.Column).End(xlUp)) 
End With 

그런 열 M 항목 확인 : 와 열 검증 이름으로 항상 존재하고 있음을

Dim cel As Range 
For Each cel In Rng 
    If cel = "BLM" Or cel = "CFG" Then 
     With ws2.Cells(cel.Row, valCol) 
      .Formula = sFormula 
      .Value = .Value 
     End With 
    End If 
Next 

이 가정을 수식이 맞습니다.
check this out to see ways of avoiding select which will greatly improve coding.

+0

정확하게는 아니지만 이걸로 뭔가를 할 수 있을지 모르지만 어쨌든 요구 사항을 언급하지 않았을 수 있습니다. – Chito

관련 문제