04. april 2011 - 18:11
												Der er
									7 kommentarer													
									
		 
		
			
Excel VBA: Select case, if ... then - eller noget helt 3.?
			Løntrin Basisløn
2         0
8         0
4         0
S         0
C         0
Jeg har brug for at kunne udfylde felterne i kolonnen 'Basisløn' med tal, baseret på hvilket løntrin der er anført i kolonnen 'løntrin'. Tallene der matcher løntrinnet hentes fra et dataark i samme excelmappe.
Nogen idéer?
					
		
	 
		
								
					
				30. april 2011 - 03:13
				#6
						
		 
		
			Hej,
Herunder et bud på en løsning
Sub Løntrin()
    Dim GRK As Long
    
    Sheets("Grund-ark").Select
    Columns(2).Select
    Selection.TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 2), TrailingMinusNumbers:=True
        
    Sheets("Løn-ark").Select
    Columns(1).Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 2), TrailingMinusNumbers:=True
    
    GRK = 2
    
    Do
    Sheets("Grund-ark").Select
    Cells(GRK, 3).Select
        ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(RC[-1],'Løn-ark'!R2C1:R20C2,2,FALSE)),""Løntrinnet eksisterer ikke"",VLOOKUP(RC[-1],'Løn-ark'!R2C1:R20C2,2,FALSE))"
    GRK = GRK + 1
    Loop Until Cells(GRK, 1) = ""
    Cells(1, 1).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Selection.Font.Bold = True
    With Selection.Interior
        .ColorIndex = 15
        .Pattern = xlSolid
    End With
    Rows("2:2").Select
    ActiveWindow.FreezePanes = True
    Cells(1, 1).Select
    Selection.CurrentRegion.Select
    Selection.EntireColumn.AutoFit
    Cells(2, 1).Select
End Sub
I ovenstående eksempel er Grund-ark et ark der består af tre kolonner:
Navn   Løntrin    Basisløn
Løn-arket er et ark der består af to kolonner:
Løntrin  Basisløn
I dette ark indtastes hhv. Løntrin og Basisløn.
hvis der kommer en ny medarbejder indtastes navn og løntrin i Grund-arket, og derefter aktiveres makroen. Nu indsættes den værdi der er sat ind ud for det valgte løntrin i kolonne tre.
Skulle man komme til at indtaste et løntrin der ikke eksisterer i Løn-arket, bliver man gjort opmærksom på fejlen.
Med venlig hilsen
Henrik